Skip to content

Commit 24dd027

Browse files
authored
Merge pull request #1653 from infosiftr/postgres-auth-method
Add section on new POSTGRES_HOST_AUTH_METHOD
2 parents 49507a9 + cf89f21 commit 24dd027

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

Diff for: postgres/content.md

+21-9
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ The PostgreSQL image uses several environment variables which are easy to miss.
5353

5454
### `POSTGRES_PASSWORD`
5555

56-
This 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.
56+
This environment variable is normally required 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.
5757

5858
Note 1: The PostgreSQL image sets up `trust` authentication locally so you may notice a password is not required when connecting from `localhost` (inside the same container). However, a password will be required if connecting from a different host/container.
5959

60-
Note 2: This variable defines the superuser password in the PostgreSQL instance, as set by the `initdb` script during inital container startup. It has no effect on the `PGPASSWORD` environment variable that may be used by the `psql` client at runtime, as described at [https://www.postgresql.org/docs/10/static/libpq-envars.html](https://www.postgresql.org/docs/10/static/libpq-envars.html). `PGPASSWORD`, if used, will be specified as a separate environment variable.
60+
Note 2: This variable defines the superuser password in the PostgreSQL instance, as set by the `initdb` script during initial container startup. It has no effect on the `PGPASSWORD` environment variable that may be used by the `psql` client at runtime, as described at [https://www.postgresql.org/docs/10/static/libpq-envars.html](https://www.postgresql.org/docs/10/static/libpq-envars.html). `PGPASSWORD`, if used, will be specified as a separate environment variable.
6161

6262
### `POSTGRES_USER`
6363

@@ -77,6 +77,18 @@ This optional environment variable can be used to define another location for th
7777

7878
**Note:** on PostgreSQL 9.x, this variable is `POSTGRES_INITDB_XLOGDIR` (reflecting [the changed name of the `--xlogdir` flag to `--waldir` in PostgreSQL 10+](https://wiki.postgresql.org/wiki/New_in_postgres_10#Renaming_of_.22xlog.22_to_.22wal.22_Globally_.28and_location.2Flsn.29)).
7979

80+
### `POSTGRES_HOST_AUTH_METHOD`
81+
82+
This optional variable can be used to control the `auth-method` for `host` connections for `all` databases, `all` users, and `all` addresses. If unspecified then [`md5` password authentication](https://www.postgresql.org/docs/current/auth-password.html) is used. On an uninitialized database, this will populate `pg_hba.conf` via this approximate line:
83+
84+
```console
85+
echo "host all all all $POSTGRES_HOST_AUTH_METHOD" >> pg_hba.conf
86+
```
87+
88+
It is not recommended to use [`trust`](https://www.postgresql.org/docs/current/auth-trust.html) since it allows anyone to connect without a password, even if one is set (like via `POSTGRES_PASSWORD`).
89+
90+
See the PostgreSQL documentation on [`pg_hba.conf`](https://www.postgresql.org/docs/current/auth-pg-hba-conf.html) for more information about possible values and their meanings.
91+
8092
### `PGDATA`
8193

8294
This optional 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 filesystem mountpoint (like with GCE persistent disks), Postgres `initdb` recommends a subdirectory (for example `/var/lib/postgresql/data/pgdata` ) be created to contain the data.
@@ -131,13 +143,13 @@ There are many ways to set PostgreSQL server configuration. For information on w
131143
$ # customize the config
132144

133145
$ # run postgres with custom config
134-
$ docker run -d --name some-postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf %%IMAGE%% -c 'config_file=/etc/postgresql/postgresql.conf'
146+
$ docker run -d --name some-postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf -e POSTGRES_PASSWORD=mysecretpassword %%IMAGE%% -c 'config_file=/etc/postgresql/postgresql.conf'
135147
```
136148

137149
- Set options directly on the run line. The entrypoint script is made so that any options passed to the docker command will be passed along to the `postgres` server daemon. From the [docs](https://www.postgresql.org/docs/current/static/app-postgres.html) we see that any option available in a `.conf` file can be set via `-c`.
138150

139151
```console
140-
$ docker run -d --name some-postgres %%IMAGE%% -c 'shared_buffers=256MB' -c 'max_connections=200'
152+
$ docker run -d --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword %%IMAGE%% -c 'shared_buffers=256MB' -c 'max_connections=200'
141153
```
142154

143155
## Locale Customization
@@ -165,11 +177,11 @@ As of [docker-library/postgres#253](https://github.com/docker-library/postgres/p
165177
The main caveat to note is that `postgres` doesn't care what UID it runs as (as long as the owner of `/var/lib/postgresql/data` matches), but `initdb` *does* care (and needs the user to exist in `/etc/passwd`):
166178

167179
```console
168-
$ docker run -it --rm --user www-data %%IMAGE%%
180+
$ docker run -it --rm --user www-data -e POSTGRES_PASSWORD=mysecretpassword %%IMAGE%%
169181
The files belonging to this database system will be owned by user "www-data".
170182
...
171183

172-
$ docker run -it --rm --user 1000:1000 %%IMAGE%%
184+
$ docker run -it --rm --user 1000:1000 -e POSTGRES_PASSWORD=mysecretpassword %%IMAGE%%
173185
initdb: could not look up effective user ID 1000: user does not exist
174186
```
175187

@@ -180,7 +192,7 @@ The three easiest ways to get around this:
180192
2. bind-mount `/etc/passwd` read-only from the host (if the UID you desire is a valid user on your host):
181193

182194
```console
183-
$ docker run -it --rm --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro %%IMAGE%%
195+
$ docker run -it --rm --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro -e POSTGRES_PASSWORD=mysecretpassword %%IMAGE%%
184196
The files belonging to this database system will be owned by user "jsmith".
185197
...
186198
```
@@ -189,7 +201,7 @@ The three easiest ways to get around this:
189201

190202
```console
191203
$ docker volume create pgdata
192-
$ docker run -it --rm -v pgdata:/var/lib/postgresql/data %%IMAGE%%
204+
$ docker run -it --rm -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=mysecretpassword %%IMAGE%%
193205
The files belonging to this database system will be owned by user "postgres".
194206
...
195207
( once it's finished initializing successfully and is waiting for connections, stop it )
@@ -222,7 +234,7 @@ The Docker documentation is a good starting point for understanding the differen
222234
2. Start your `%%IMAGE%%` container like this:
223235

224236
```console
225-
$ docker run --name some-%%REPO%% -v /my/own/datadir:/var/lib/postgresql/data -d %%IMAGE%%:tag
237+
$ docker run --name some-%%REPO%% -v /my/own/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=mysecretpassword -d %%IMAGE%%:tag
226238
```
227239

228240
The `-v /my/own/datadir:/var/lib/postgresql/data` part of the command mounts the `/my/own/datadir` directory from the underlying host system as `/var/lib/postgresql/data` inside the container, where PostgreSQL by default will write its data files.

0 commit comments

Comments
 (0)