postgresql - 如何通过 .sh 文件添加 Postgres 扩展

标签 postgresql shell psql

我正在尝试通过编写以下脚本 (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 东西来运行上面的代码。此外,当我手动输入这些命令时,它成功创建了扩展。感谢所有提供帮助的人。

最佳答案

尝试:

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

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

-c command

--command=command

Specifies that psql is to execute the given command string, command.

对于更复杂的脚本,也可以考虑使用 -f sql_file.sql,或者像这样:

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

关于postgresql - 如何通过 .sh 文件添加 Postgres 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44753182/

相关文章:

postgresql - Postgres 循环每个注册表并在另一个表中搜索

SQL根据多个字段删除重复行

bash - 使用 nc 传输文件时的空文件

postgresql - 需要在 postgresql 的不同 session 中使用一个临时表

postgresql - 如何查看 Google Cloud 上永久性磁盘的剩余大小?我在哪里可以找到实例中的代码?

java - IS NULL 条件下的 JDBC 错误

shell - 以秒为单位获取文件修改时间的最佳方法

用于集成到 CI 管道中的 Android Studio 代码检查 shell 脚本

postgresql - tuple_only 模式下的 Raise Notice 函数

php - Laravel 中 pgsql 上的 Cacti