The area51/postgresql image contains the current PostgreSQL 9.5 database engine with additional configuration support.


To run a new instance, start a container as follows:

docker run --name postgresql -e POSTGRES_PASSWORD=password -d area51/postgresql

You can then use a normal postgresql client to access it.

To get it's address:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' postgresql

This will return an IPv4 address:

Then you can connect to it with:

psql -h -U postgres

If you are using IPv6 you can use the following to get at the ip address:

docker inspect --format '{{ .NetworkSettings.GlobalIPv6Address }}' postgresql

This will return something like 2001:DB8::242:ac11:454

Advanced networking

When you run it you can add additional entries into pg_hba.conf to allow external networks access. By default we add the local docker network to this but you can add more.

To add more, simply include the POSTGRES_NETWORK environment variable, so to add the 2001:DB8:1234/48 network then we would use:

docker run --name postgresql \
   -e POSTGRES_PASSWORD=password \
   -e POSTGRES_NETWORK="2001:DB8:1234/48" \
   -d area51/postgresql

You can add more to this, just separate them with spaces. You can add IPv4 addresses here as well:

docker run --name postgresql \
   -e POSTGRES_PASSWORD=password \
   -e POSTGRES_NETWORK="2001:DB8:1234/48 2001:DB8:9999:1212/64" \
   -d area51/postgresql

This will then add 3 networks to the pg_hba.conf file.

Environment Variables

These are inherited from the official postgres image this image is based on.

POSTGRES_PASSWORDThis environment variable is recommended for you to use the PostgreSQL image. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable. In the above example, it is being set to "mysecretpassword".
POSTGRES_USERThis optional environment variable is used in conjunction with POSTGRES_PASSWORD to set a user and its password. This variable will create the specified user with superuser power and a database with the same name. If it is not specified, then the default user of postgres will be used.
PGDATAThis optional environment variable can be used to define another location - like a subdirectory - for the database files. The default is /var/lib/postgresql/data, but if the data volume you're using is a fs mountpoint (like with GCE persistent disks), Postgres initdb recommends a subdirectory (for example /var/lib/postgresql/data/pgdata ) be created to contain the data.
POSTGRES_DBThis optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.
POSTGRES_INITDB_ARGSThis optional environment variable can be used to send arguments to postgres initdb. The value is a space separated string of arguments as postgres initdb would expect them. This is useful for adding functionality like data page checksums: -e POSTGRES_INITDB_ARGS="--data-checksums".

How to extend this image

This is exactly the same as the official image,c so the documentation in this section is the same as that image. The only difference is that the /docker-entrypoint-initdb.d directory will already exist so there's no need to create it.

If you would like to do additional initialization in an image derived from this one, add one or more *.sql or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.

For example, to add an additional user and database, add the following to /docker-entrypoint-initdb.d/

set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
   CREATE USER docker;

These initialization files will be executed in sorted name order as defined by the current locale, which defaults to en_US.utf8. Any *.sql files will be executed by POSTGRES_USER, which defaults to the postgres superuser. It is recommended that any psql commands that are run inside of a *.sh script be executed as POSTGRES_USER by using the --username "$POSTGRES_USER" flag. This user will be able to connect without a password due to the presence of trust authentication for Unix socket connections made inside the container.

Digging around

Once running you can get a shell with:

docker exec -it postgresql /bin/bash