VRT Systems Docker PostgreSQL
A postgres image made with a Dockerfile derived from the official Postgres docker image, using vrtsystems/baseimage and the supervisord entry point.
The image pulls in the PostgreSQL 9.3 series.
How to use this image
start a postgres instance
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d vrtsystems/postgres
This image includes
EXPOSE 5432 (the postgres port), so standard container linking will make it automatically available to the linked containers. The default
postgres user and database are created in the entrypoint with
connect to it from an application
docker run --name some-app --link some-postgres:postgres -d application-that-uses-postgres
... or via
docker run -it --link some-postgres:postgres --rm postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
This image implements the same environment variables as the official PostgreSQL image:
POSTGRES_PASSWORD- sets the superuser password for PostgreSQL. The default superuser is defined by the
POSTGRES_USERenvironment variable. In the above example, it is being set to "mysecretpassword".
POSTGRES_USER- used in conjunction with
POSTGRES_PASSWORDto 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
postgreswill be used.
There are also three variables that control the
supervisord instance to allow
starting and stopping the server for testing purposes.
SUPERVISOR_XMLRPC_PORT: TCP port number used for XML-RPC. If set, this
enables XML-RPC on the nominated port. The suggested port for this is 9001
supervisorddocumentation; this is
EXPOSEd to other containers.
SUPERVISOR_XMLRPC_USERNAME: If set, this defines the username required for
authentication with XML-RPC. Otherwise, no authentication is required.
SUPERVISOR_XMLRPC_PASSWORD: If set, this defines the password required for
authentication with XML-RPC.
How to extend this image
With a Dockerfile
With a shell script in prestart-init.d
If you would like to do additional initialization before the database is started, add a
*.sh script under
/usr/local/etc/docker-postgres/prestart-init.d/. After the entrypoint calls
initdb to create the user and database, it will source any
*.sh script found in that directory to do further initialization before starting the service. The database is not running at this point. If you need to execute SQL commands as part of your initialization, the use of Postgres'' single user mode is recommended, but note that it comes with limitations.
With a shell script in poststart-init.d
In addition to the prestart hook above, this image provides a further init capability specifically for running SQL scripts against a full version of the database server. Add
*.sh scripts under
/usr/local/etc/docker-postgres/poststart-init.d/ and once the database is started these will be sourced and run with the postgres system user. Note that you can access
POSTGRES_DB in these scripts to run commands against the specific database. An example of how to execute SQL against the running database:
#!/bin/sh psql -h localhost -d $POSTGRES_DB -U $POSTGRES_USER <<- EOSQL CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology; CREATE EXTENSION temporal_tables; EOSQL
If there is no database when
postgres starts in a container, then
postgres will create the default database for you. While this is the expected behavior of
postgres, this means that it will not accept incoming connections during that time. This may cause issues when using automation tools, such as
fig), that start several containers simultaneously.