【问题标题】:How to add Postgres extensions via .sh file如何通过 .sh 文件添加 Postgres 扩展
【发布时间】:2017-11-28 21:47:16
【问题描述】:

我正在尝试通过编写以下脚本 (setup.sh) 为 Postgres 添加扩展:

sudo -u postgres psql my_database
CREATE EXTENSION adminpack;

当我做 vagrant up 时,它应该通过运行脚本来自动运行和添加扩展。但是,我收到了

的错误消息
==> default: ERROR:  syntax error at or near "exit"
==> default: LINE 1: exit
==> default:         ^
==> default: /tmp/vagrant-shell: line 24: CREATE: command not found

请注意,我已经安装了所有必要的 postgres 东西来运行上面的代码。另外,当我手动输入这些命令时,它成功创建了扩展。感谢任何提供帮助的人。

【问题讨论】:

    标签: postgresql shell psql


    【解决方案1】:

    尝试:

    sudo -u postgres psql my_database -c "CREATE EXTENSION adminpack"
    

    https://www.postgresql.org/docs/current/static/app-psql.html

    -c 命令

    --command=command

    指定psql要执行给定的命令字符串command。

    还可以考虑将-f sql_file.sql 用于更复杂的脚本,或者类似:

    psql <<EOF
    \x
    SELECT NOW();
    SELECT * FROM foo;
    EOF
    

    【讨论】:

      猜你喜欢
      • 2022-10-17
      • 2012-07-26
      • 2021-09-01
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2021-10-18
      • 1970-01-01
      相关资源
      最近更新 更多