18 May 2019
This is a simple post related to Docker container images and exactly how you can extend the official postgres image (in this post the 9.6 release) to create custom users and restore backups.
All the customizations (bash scripts and DBs) are copied inside the
/docker-entrypoint-initdb.d/ path inside the container.
So for the scope of this example we will need:
It’s very important to remember that everything in
docker-entrypoint-initdb.d folder will be executed by its name so keep this in mind(names that start with a number will be executed before everything else).
This is the structure of the directories:
My-personal-postgresql-image |__overwrites/ |__init-user-db-sh |__my-db.sql.gz Dockerfile
Here is an example of the Dockerfile configuration:
FROM postgres:9.6 LABEL MAINTAINER Your Name <[email protected]> COPY overwrites/ /docker-entrypoint-initdb.d/ EXPOSE 5432
This way when the container starts, users and DBs are created from scratch.
Following is an example of the
init-user-db.sh script :
#!/bin/bash set -e psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL CREATE USER my-user; ALTER ROLE my-user WITH PASSWORD 'my-password'; GRANT ALL PRIVILEGES ON DATABASE my-db TO my-user; EOSQL