如何从批处理文件中执行 postgres 的 sql 查询?
How to execute postgres' sql queries from batch file?
我需要从批处理文件中执行 SQL。
我正在执行以下操作以连接到 Postgres 和来自 table
的 select 数据
C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
select * from test;
我可以连接到数据库,但是出现错误
'select' is not recognized as an internal or external command,
operable program or batch file.
有人遇到过这样的问题吗?
这是我正在尝试的查询之一,类似的东西在 shell 脚本中起作用,(如果有的话,请忽略查询中的语法错误)
copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;
您不能将查询放在单独的行中,批处理解释器将假定它是另一个命令而不是对 psql 的查询。我相信你也需要引用它。
使用-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
-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).
1] 如果您使用 sql 传递文件,请使用 -f
或 --file
参数
2] 如果您要传递单个命令,请使用 -c
或 --command
参数
您可以将其通过管道传输到 psql
(
echo select * from test;
) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
当右括号是 SQL 查询的一部分时,它们必须用三个脱字符转义。
(
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
我同意蜘蛛侠的观点:
1] if you are passing the file with the sql use -f or --file parameter
当你想执行多个命令时,最好的方法是添加参数 -f,然后只需键入你的文件路径,不带任何 " 或 ' 标记(相对路径也适用):
psql -h %host% -p 5432 -U %user% -d %dbname% -f ..\..\folder\Data.txt
它也适用于 .NET Core。我需要它在迁移后将基本数据添加到我的数据库中。
如果您正在尝试 shell 脚本
psql postgresql://$username:$password@$host/$database < /app/sql_script/script.sql
如果 运行 在 Linux 上,这对我有用(需要用您的用户、数据库名称等更新下面的值)
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"
我需要从批处理文件中执行 SQL。 我正在执行以下操作以连接到 Postgres 和来自 table
的 select 数据C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
select * from test;
我可以连接到数据库,但是出现错误
'select' is not recognized as an internal or external command, operable program or batch file.
有人遇到过这样的问题吗?
这是我正在尝试的查询之一,类似的东西在 shell 脚本中起作用,(如果有的话,请忽略查询中的语法错误)
copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;
您不能将查询放在单独的行中,批处理解释器将假定它是另一个命令而不是对 psql 的查询。我相信你也需要引用它。
使用-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
-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).
1] 如果您使用 sql 传递文件,请使用 -f
或 --file
参数
2] 如果您要传递单个命令,请使用 -c
或 --command
参数
您可以将其通过管道传输到 psql
(
echo select * from test;
) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
当右括号是 SQL 查询的一部分时,它们必须用三个脱字符转义。
(
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
我同意蜘蛛侠的观点:
1] if you are passing the file with the sql use -f or --file parameter
当你想执行多个命令时,最好的方法是添加参数 -f,然后只需键入你的文件路径,不带任何 " 或 ' 标记(相对路径也适用):
psql -h %host% -p 5432 -U %user% -d %dbname% -f ..\..\folder\Data.txt
它也适用于 .NET Core。我需要它在迁移后将基本数据添加到我的数据库中。
如果您正在尝试 shell 脚本
psql postgresql://$username:$password@$host/$database < /app/sql_script/script.sql
如果 运行 在 Linux 上,这对我有用(需要用您的用户、数据库名称等更新下面的值)
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"