如何从批处理文件执行 postgres 的 sql 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34880453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute postgres' sql queries from batch file?
提问by Ketu
I need to execute SQL from batch file. I am executing following to connect to Postgres and select data from table
我需要从批处理文件中执行 SQL。我正在执行以下操作以连接到 Postgres 并从表中选择数据
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.
'select' 不是内部或外部命令,也不是可运行的程序或批处理文件。
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)
这是我正在尝试的查询之一,类似的东西在 shell 脚本中起作用,(如果有任何语法错误,请忽略查询中的语法错误)
copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;
采纳答案by jeb
You could pipe it into psql
你可以把它用管道传送到 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.
当右括号是 SQL 查询的一部分时,它们必须用三个插入符号进行转义。
(
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
回答by greygreg87
I agree with Spidey:
我同意蜘蛛侠的观点:
1] if you are passing the file with the sql use -f or --file parameter
1] 如果您使用 sql 传递文件,请使用 -f 或 --file 参数
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):
当您想执行多个命令时,最好的方法是添加参数 -f,然后只需键入文件的路径,而无需任何 " 或 ' 标记(相对路径也适用):
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.
它也适用于 .NET Core。我需要它在迁移后将基本数据添加到我的数据库中。
回答by Akhilraj N S
If you are trying the shell script
如果您正在尝试 shell 脚本
psql postgresql://$username:$password@$host/$database < /app/sql_script/script.sql
psql postgresql://$username:$password@$host/$database < /app/sql_script/script.sql
回答by LeleDumbo
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.
您不能将查询放在单独的行上,批处理解释器将假定它是另一个命令,而不是对 psql 的查询。我相信你也需要引用它。
回答by Clodoaldo Neto
Use the -f
parameter to pass the batch file name
使用-f
参数传递批处理文件名
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
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).
-f 文件名
--file=文件名
使用文件文件名作为命令的来源,而不是交互地读取命令。处理文件后,psql 终止。这在很多方面等同于元命令 \i。
如果文件名是 -(连字符),则读取标准输入,直到出现 EOF 指示或 \q 元命令。但是请注意,在这种情况下不使用 Readline(就像指定了 -n 一样)。
回答by Techie
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
1] 如果您使用 sql 使用-f
或--file
参数传递文件
2] 如果您传递单个命令使用-c
或--command
参数