How to execute postgres' sql queries from batch file?
Asked Answered
C

7

15

I need to execute SQL from batch file. I am executing following to connect to Postgres and select data from table

C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% 
select * from test;

I am able to connect to database, however I'm getting the error

'select' is not recognized as an internal or external command, operable program or batch file.

Has anyone faced such issue?

This is one of the query i am trying, something similar works in shell script, (please ignore syntax error in the query if there are any)

copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;
Catrinacatriona answered 19/1, 2016 at 15:18 Comment(1)
As mentioned below, try adding -c parameter before the sql commandIsidoro
C
14

You could pipe it into psql

(
echo select * from test;
) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% 

When closing parenthesis are part of the SQL query they have to be escaped with three carets.

( 
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
Catamount answered 19/1, 2016 at 15:51 Comment(4)
this is the current query I'm trying, ( echo insert into testconfig(testid,scenarioid,testname) values( 1,1,'asdf'); ) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% and i'm getting value( was unexpected at this time. any suggestions?Catrinacatriona
Yes, see my second sample. you need three carets per closing parenthesis. ...(name1 ^^^) values(value1 ^^^)Catamount
how can i save the output in csv file in windows ??Pretermit
( echo SELECT * FROM docteur; ) | C:\"Program Files"\PostgreSQL\9.5\bin\psql -h localhost -p 5433 -U postgres -d docarriv_db Here is an example ( windows 10, PostgreSQL 9.6) according to this answer.Plummet
H
4

Use the -f parameter to pass the batch file name

C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% -f 'sql_batch_file.sql'

http://www.postgresql.org/docs/current/static/app-psql.html

-f filename

--file=filename

Use the file filename as the source of commands instead of reading commands interactively. After the file is processed, psql terminates. This is in many ways equivalent to the meta-command \i.

If filename is - (hyphen), then standard input is read until an EOF indication or \q meta-command. Note however that Readline is not used in this case (much as if -n had been specified).

Helm answered 19/1, 2016 at 15:42 Comment(3)
queries that that needs to be executed have batch variables in them as data. i am not very sure if external sql file can be used with this.Catrinacatriona
@ketub: Post a sample of the queries you want to execute in the question.Helm
copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv; This is one of the query i am trying, something similar works in shell script, (please ignore syntax error in the query if there are any)Catrinacatriona
D
3

if running on Linux, this is what worked for me (need to update values below with your user, db name etc)

psql "host=YOUR_HOST port=YOUR_PORT dbname=YOUR_DB_NAME user=YOUR_USER_NAME password=YOUR_PASSWORD" -f "fully_qualified_path_to_your_script.sql"
Definitive answered 9/6, 2020 at 21:13 Comment(1)
The -f part fails on 16.Ochlocracy
B
1

You cannot put the query on separate line, batch interpreter will assume it's another command instead of a query for psql. I believe you will need to quote it as well.

Brinker answered 19/1, 2016 at 15:22 Comment(2)
in Shell Script i used << EOF sql queries sql queries sql queries EOF does batch has something on similar grounds?Catrinacatriona
@ketub the "HereDoc" << EOF does not work on WindowsLogomachy
M
1

I agree with Spidey:

1] if you are passing the file with the sql use -f or --file parameter

When you want to execute several commands the best way to do that is to add parameter -f, and after that just type path to your file without any " or ' marks (relative paths works also):

psql -h %host% -p 5432 -U %user% -d %dbname% -f ..\..\folder\Data.txt

It also works in .NET Core. I need it to add basic data to my database after migrations.

Missie answered 20/7, 2018 at 7:30 Comment(0)
I
0

Kindly refer to the documentation

1] if you are passing the file with the sql use -f or --file parameter
2] if you are passing individual command use -c or --command parameter

Isidoro answered 19/1, 2016 at 15:45 Comment(0)
H
0

If you are trying the shell script

psql postgresql://$username:$password@$host/$database < /app/sql_script/script.sql

Horoscopy answered 17/9, 2019 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.