如何从批处理文件执行postgres的sql查询? [英] How to execute postgres' sql queries from batch file?

查看:203
本文介绍了如何从批处理文件执行postgres的sql查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从批处理文件执行SQL。
我正在执行以下操作,以连接到Postgres并从表中选择数据

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'不被识别为内部或外部命令,
可操作程序或批处理文件。

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

有人遇到过这样的问题吗?

Has anyone faced such issue?

这是我正在尝试的查询之一,在shell脚本中类似的功能, (请忽略查询中的语法错误,如果有的话)

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;


推荐答案

您可以将其通过管道传输到psql

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% 

当右括号是SQL查询的一部分时,必须使用三个脱字符将它们转义。

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%

这篇关于如何从批处理文件执行postgres的sql查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆