PostgreSQL 将查询作为字符串代替 sql 文件运行

mek*_*oke -2 postgresql bash psql

出于维护原因,我需要使用 psql 运行查询,如下所示

psql -U postgres -d test 'update sometbl set col1 = true;'
Run Code Online (Sandbox Code Playgroud)

我可以将查询放入 sql 文件中并使用 -f 选项运行它,但我确实需要从 bash 脚本中运行它,并且不想为这个简单的查询使用额外的 sql 文件。

小智 5

您可以通过 psql 命令行界面使用参数执行命令-c

所以在你的例子中,这将是:

psql -U postgres -d test -c 'update sometbl set col1 = true;'
Run Code Online (Sandbox Code Playgroud)

请参阅此处的 psql 文档: https: //www.postgresql.org/docs/9.2/app-psql.html