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

我使用docker postgres 9.4.5镜像,并使用init.sh将其扩展到/docker-entrypoint-initdb.d容器中的/docker-entrypoint-initdb.d 。 我试图创build另一个数据库和一个非pipe理员用户,作为访问$POSTGRES_DB以及我创build的第二个数据库。 我尝试以下$POSTGRES_USER是用户myadmin$POSTGRES_DB是通过mydb1 -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 

然后我尝试创build数据库userx:#!/ bin / bash set -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 

有人可以请帮助,这是一个真正的阻碍我,我不知道如何进行,因为我觉得我不应该需要创build数据库userx或angular色,或者我错了吗?

你的GRANT语法不太对 – https://www.postgresql.org/docs/9.0/static/sql-grant.html

总之应该是这样的:

 GRANT ALL PRIVILEGES ON yourdbname not user TO your user;