I'm using docker postgres 9.4.5 images and extendeding it using an init.sh copied to /docker-entrypoint-initdb.d in the docker container. I am trying to create another database and a non admin user that as access to both $POSTGRES_DB as well as the 2nd database I am creating.
I tried the following where $POSTGRES_USER is the user myadmin and $POSTGRES_DB is the database mydb1 passed through docker-compose environment:
#!/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
This gave me an error:
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
I then tried it with --username postgres and now get the error
ERROR: database "userx" does not exist
I then tried to create the database 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
but got:
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
Can someone please help, this is a real blocker for me and I have no idea how to proceed as I feel that I should not need to be creating the database userx or the roles , or am I wrong?