【问题标题】:How do I add '\c' to a psql command executed from a bash script?如何将 '\c' 添加到从 bash 脚本执行的 psql 命令中?
【发布时间】:2021-10-03 11:56:06
【问题描述】:

要设置我的项目,我必须执行很多命令,并且我正在尝试编写脚本。其中一些在 psql 中;所以通常我会去

psql -U postgres -h localhost -p 5433
(psql) create database test_database
(psql) \c test_database
(psql) \i integration-test/src/test/resources/init.sql 

.init.sql 包含用模拟数据填充数据库的内容。

在我的 bash 脚本中,我尝试将其减少到

psql -U postgres -h localhost -p 5433 -c "create database test_database; \c test_database; \i integration-test/src/test/resources/init.sql"

但是,这让我很兴奋

ERROR:  syntax error at or near "\"
LINE 1: create database test_database; \c fcs_analytics; \i integrat...
                                   ^

如何从我的脚本中正确执行这些命令?

【问题讨论】:

    标签: bash shell psql


    【解决方案1】:

    你试过了吗?

    psql -U postgres -h localhost -p 5433 << 'EOF'
    create database test_database
    \c test_database
    \i integration-test/src/test/resources/init.sql
    EOF
    

    【讨论】:

    • 谢谢!所以将多行命令作为字符串附加是不可行的吗?
    • 将多行命令作为字符串附加仍然可以工作,但处理引号和反斜线太麻烦了。
    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多