From e070bf299898dd676370ea735362299755e3050d Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 15 Feb 2016 16:46:23 -0800 Subject: [PATCH] Add "psql" initdb example to postgres --- postgres/content.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/postgres/content.md b/postgres/content.md index c42cce740..ce8930645 100644 --- a/postgres/content.md +++ b/postgres/content.md @@ -57,6 +57,19 @@ This optional environment variable can be used to define a different name for th 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/init-user-db.sh`: + +```bash +#!/bin/bash +set -e + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE USER docker; + CREATE DATABASE docker; + GRANT ALL PRIVILEGES ON DATABASE docker TO docker; +EOSQL +``` + 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. You can also extend the image with a simple `Dockerfile` to set a different locale. The following example will set the default locale to `de_DE.utf8`: