How to execute multiple queries using psql command from bash shell? - database

I need to execute postgresql queries from command line using psql -c command.
For every psql command, it opens a new tcp connection to connect to the database server and execute query which is a overhead for large number of queries.
Currently I can execute single query like this:
psql -U postgres -h <ip_addr> -c "SELECT * FROM xyz_table;"
When I tried to execute multiple queries as below, but only the last query got executed.
psql -U postgres -h <ip_addr> -c "SELECT * FROM xyz_table; SELECT * FROM abc_table;"
Can anyone help me and tell me the proper way to do it?

-c processes only one command. Without it however psql expects commands to be passed into standard input, e.g.:
psql -U postgres -h <ip_addr> <database_name> << EOF
SELECT * FROM xyz_table;
SELECT * FROM abc_table;
EOF
Or by using echo and pipes.

at least from 9.6.2 this approach works as well:
psql -c "select now()" -c "select version()" -U postgres -h 127.0.0.1
now
2017-12-26 20:25:45.874935+01
(1 row)
version
PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.3.1-14ubuntu2) 5.3.1 20160413, 64-bit
(1 row)

Using echo and a pipe to fit it on a single line:
echo 'SELECT * FROM xyz_table; \n SELECT * FROM abc_table' | psql -U postgres

The --file parameter executes a file's content
psql -U postgres -h <ip_addr> -f "my_file.psql"
All the output will be sent to standard output
http://www.postgresql.org/docs/current/static/app-psql.html

Related

How to export data from sql to excel using batch - exporting one column in sql per one column in excel?

I am exporting sql view using .bat script:
sqlcmd -U HPH_COM -P HPH_COM -S vTF-DB\LEVEL2 -d HPH_COM -Q " SELECT TOP (1000) * FROM HPH_COM.dbo.REP_TEMP_STOCKS_LASTDAY " -o "C:\sajtovi\TF1_PyroMeasuresLastDay.csv"
And its working. However, exported csv file is not organized as when you export using SSMS: using batch one column in sql is not copied to one column in excel, but more columns from sql to one column in excel. so i cannot play with exported data. Any idea how to write script which will give same results as exported with ssms?
Thanks
you can use these options.
-h rows_per_header
-s col_separator
-W (remove trailing spaces)
and also specify set nocount on to prevent the completion message from appearing.
It looks like this.
sqlcmd -U HPH_COM -P HPH_COM -S vTF-DB\LEVEL2 -d HPH_COM -Q "set nocount on; SELECT TOP (1000) * FROM HPH_COM.dbo.REP_TEMP_STOCKS_LASTDAY " -o "C:\sajtovi\TF1_PyroMeasuresLastDay.csv" -W -h -1 -s,

BCP with input file

I would like to export data with BCP
Below is my command
bcp queryout -i "test.sql" -o"myTable.csv" -S "server\Db" -E /c /t, -T
test.sql has the SQL statemement. I need to keep the SQL in a file as the statement is rather long
I have tested the SQL to be returning values in the management studio
But I get the below errors in the command prompt
Copy direction must be either 'in', 'out' or 'format'.
I have also tried the below variations without much luck
bcp out -i "test.sql" -o"myTable.csv" -S "server\Db" -E /c /t, -T
bcp -i "test.sql" out -o"myTable.csv" -S "server\Db" -E /c /t, -T
bcp -i "test.sql" queryout -o"myTable.csv" -S "server\Db" -E /c /t, -T
The -i specifies input data file. The query has to be part of the command line in the queryout. The bcp tool is quite ancient and didn't improve over the years much.
You might use different tools to do the expert. BCP shines when you need to import data, but exporting can be done efficiently using many other tools.
To output in csv format you can use sqlcmd see How to export data as CSV format from SQL Server using sqlcmd?

-X option not disable sql commands in sqlcmd

I use sqlcmd to execute large sql file which insert multiple of records into database. So when sqlcmd run it display a error message like "syntax error near 'Ed'..." . I known Ed is sql commands and i found -X will disable that kind of commands but it is not work.
My command like this:
sqlcmd -S "tcp:ip,1433" -U "sa" -P "pass" -d "dbname" -c "GO" -k 1 -f 65001 -i "C:\sql.sql" -X -x
My sql file content:
Go
insert into tablename (title) values (N'title
Ed some more data
some more data
some more data')
Go
insert into tablename (title) values (N'title
Ed some more data
some more data
some more data')
Go
Could you give me some help? please.
Thanks you.
It use -X[1] instead of -X.
sqlcmd -S "tcp:ip,1433" -U "sa" -P "pass" -d "dbname" -c "GO" -k 1 -f 65001 -i "C:\sql.sql" -X[1]

BCP utility:"Copy direction must be either 'in', 'out' or 'format'"

I get below mentioned error when I run BCP command for trusted connection:
Copy direction must be either 'in', 'out' or 'format'.
I tried searching MSDN, where it specifies that servername passed could be incorrect.
The command I am trying is:
bcp %SQL_database%..TABLE1 in \FileSERVER\file.dat -f\fileserver\Formats\file.fmt -eERR.txt -m1000000 -C -T RAW -S%SQL_server%
When I pass a username and password instead of using the -T option, it works. The command is executed from command prompt by passing parameters from command line.
Your -C and -T options are flip-flopped - -C -T RAW instead of -C RAW -T.
Check the bcp utility's online documentation for confirmation that -C rather than -T should precede RAW.
Try this instead:
bcp %SQL_database%..TABLE1 in \FileSERVER\file.dat -f\fileserver\Formats\file.fmt -eERR.txt -m1000000 -C RAW -T -S%SQL_server%
My guess is that you probably misplaced the -T option when switching to a trusted connection (with the -T option) from integrated security (with the -U and -P options).

how do i pass in a command to sqsh and get the output to a file in one go?

I'm trying to set up a simple loop to periodically query a database table in bash. Normally I seem to have to do:
sqsh -s SERV -U user -P passwd -D db -L bcp_colsep=','
then within sqsh I have to type:
select * from some_table where foo=bar
\go -m bcp > /path/to/output.out
I was trying to use the -C option to sqsh to pass in the command like this:
sqsh -s SERV -U user -P passwd -D db -L bcp_colsep=',' -C 'select * from some_table where foo=bar \go -m bcp > /path/to/output.out'
but I keep getting:
Incorrect syntax near '\'.
How can I get the desired effect?
When you use the -C option to pass on a SQL statement to sqsh, the \go command will be implicitly executed. To get the output in 'bcp' result style you need to set the variable 'style=bcp' using the -L parameter or use -mbcp as a commandline parameter and just redirect the output to a file, or use the sqsh -o parameter to specify a filename for output. So basically your command would look like:
sqsh -S SERV -U user -P passwd -D db -L bcp_colsep=',' -m bcp \
-C 'select * from some_table where foo=bar' > /path/to/output.out
HTH, Martin

Resources