【问题标题】:Adding postgresql database import command to creation将 postgresql 数据库导入命令添加到创建
【发布时间】:2013-08-24 10:40:00
【问题描述】:

这是我用于创建数据库的 shell 命令。它作为部署脚本的一部分运行,无需人工干预即可自动创建两个数据库。

# POSTGRES
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb db1 --owner deploy"
su postgres -c "createdb db2 --owner deploy"
service postgresql reload

在这段代码中,有人可以解释一下我如何在这个阶段将 SQL 文件导入到 postgresql 中。

我相信它是这样的,但我还没有去工作:

psql --username=postgres < /etc/schema.sql

【问题讨论】:

  • 以非特权操作系统用户身份启动 psql -U postgres 假定它可以作为数据库超级用户登录,这不是默认的安全策略。为什么使用sudo/su postgres然后不使用不一致?
  • Daniel,因为我使用了来自不同地方的代码,我不完全确定自己在做什么。

标签: sql postgresql import schema debian


【解决方案1】:

在 debian 上,您可能需要遵循 Daniel 的评论,因为 PostgreSQL 可能配置为要求 OS 用户与 db 用户的用户名相同。

所以你需要

su postgres -c "psql -f /etc/schema.sql"

或者,您可以将所有这些数据库创建命令放在一个漂亮的小 shell 脚本中(没有 su postgres -c 部分),然后一次运行所有这些命令。比如:

 #!/bin/bash
 echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | su postgres -c psql
 createdb db1 --owner deploy
 createdb db2 --owner deploy
 psql db1 -f /etc/schema.sql
 psql db2 -f /etc/schema.sql

然后您可以使用 sudo 或 su -c 运行它,并简化可能出错的事情,授权方式。

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2012-10-20
    • 1970-01-01
    • 2016-10-16
    • 2011-08-09
    相关资源
    最近更新 更多