在 docker-entrypoint.sh 中为特定数据库创建模式

Create Schema for a specific Database in docker-entrypoint.sh

如何在 docker 的 shell 脚本 docker-entrypoint.sh 中为特定数据库创建新架构。

sudo -u postgres psql -c "ALTER user postgres WITH PASSWORD 'postgres'"
sudo -u postgres psql -c "CREATE DATABASE example;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE example TO postgres;"
sudo -u postgres psql -c "CREATE SCHEMA production;"
sudo -u postgres psql -c "\dn"

在 postgres 中没有像 mysql 这样的 "use database",所以我如何将它指定给 example 数据库。目前,新模式 production 默认进入 postgres 中的 postgres 数据库。

如果未指定,psql 连接到与用户同名的数据库。如果您不想这样做,请提供它应该连接到的数据库名称:

sudo -u postgres psql -d example -c "CREATE SCHEMA production;"