为什么我不能扩展docker postgres图像来创建额外的数据库和用户

时间:2016-11-17 14:43:21

标签: postgresql docker

我使用docker postgres 9.4.5图像并使用复制到docker容器中的/docker-entrypoint-initdb.d的init.sh进行扩展。我正在尝试创建另一个数据库和一个非管理员用户,作为$POSTGRES_DB以及我正在创建的第二个数据库的访问权限。 我尝试了以下操作:$POSTGRES_USER是用户myadmin,而$POSTGRES_DB是通过docker-compose环境传递的数据库mydb1

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
   CREATE USER userx WITH password '$POSTGRES_PASSWORD';
   CREATE DATABASE diagnostics;
   GRANT ALL PRIVILEGES ON DATABASE userx TO $POSTGRES_DB;
   GRANT ALL PRIVILEGES ON DATABASE userx TO mydb2;
EOSQL

这给了我一个错误:

postgres_1   | CREATE DATABASE
postgres_1   | 
postgres_1   | CREATE ROLE
postgres_1   | 
postgres_1   | 
postgres_1   | /docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init-user-db.sh
postgres_1   | FATAL:  database "myadmin" does not exist
postgres_1   | psql: FATAL:  database "myadmin" does not exist
docker_postgres_1 exited with code 2

然后我用--username postgres尝试了它,现在得到错误

ERROR:  database "userx" does not exist

然后我尝试创建数据库userx:     #!/斌/庆典     设置-e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
   CREATE DATABASE userx;       
   CREATE USER userx WITH password '$POSTGRES_PASSWORD';
   CREATE DATABASE diagnostics;
   GRANT ALL PRIVILEGES ON DATABASE userx TO $POSTGRES_DB;
   GRANT ALL PRIVILEGES ON DATABASE userx TO mydb2;
EOSQL

但得到了:

postgres_1   | CREATE DATABASE
postgres_1   | ERROR:  role "mydb1" does not exist
postgres_1   | STATEMENT:  GRANT ALL PRIVILEGES ON DATABASE rwx TO analytics;
postgres_1   | ERROR:  role "mydb2" does not exist
docker_postgres_1 exited with code 3

有人可以请求帮助,这对我来说是一个真正的阻碍,我不知道如何继续,因为我觉得我不需要创建数据库userx或角色,或者我错了吗?

1 个答案:

答案 0 :(得分:2)

您的GRANT语法不太正确 - https://www.postgresql.org/docs/9.0/static/sql-grant.html

总之应该是这样的:

GRANT ALL PRIVILEGES ON yourdbname not user TO your user;
相关问题