Skip to content

Commit 63558be

Browse files
authored
Merge pull request #802 from infosiftr/postgres-any-user
Add some notes about the caveats for "arbitrary user" support in postgres
2 parents bfc56d5 + 40a4f63 commit 63558be

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

postgres/content.md

+41
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,47 @@ This optional environment variable can be used to define a different name for th
6666

6767
This 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"`.
6868

69+
## Arbitrary `--user` Notes
70+
71+
As of [docker-library/postgres#253](https://github.com/docker-library/postgres/pull/253), this image supports running as a (mostly) arbitrary user via `--user` on `docker run`.
72+
73+
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`):
74+
75+
```console
76+
$ docker run -it --rm --user www-data postgres
77+
The files belonging to this database system will be owned by user "www-data".
78+
...
79+
80+
$ docker run -it --rm --user 1000:1000 postgres
81+
initdb: could not look up effective user ID 1000: user does not exist
82+
```
83+
84+
The two easiest ways to get around this:
85+
86+
1. bind-mount `/etc/passwd` read-only from the host (if the UID you desire is a valid user on your host):
87+
88+
```console
89+
$ docker run -it --rm --user "$(id -u):$(id -g)" -v /etc/passwd:/etc/passwd:ro postgres
90+
The files belonging to this database system will be owned by user "jsmith".
91+
...
92+
```
93+
94+
2. initialize the target directory separately from the final runtime (with a `chown` in between):
95+
96+
```console
97+
$ docker volume create pgdata
98+
$ docker run -it --rm -v pgdata:/var/lib/postgresql/data postgres
99+
The files belonging to this database system will be owned by user "postgres".
100+
...
101+
( once it's finished initializing successfully and is waiting for connections, stop it )
102+
$ docker run -it --rm -v pgdata:/var/lib/postgresql/data bash chown -R 1000:1000 /var/lib/postgresql/data
103+
$ docker run -it --rm --user 1000:1000 -v pgdata:/var/lib/postgresql/data postgres
104+
LOG: database system was shut down at 2017-01-20 00:03:23 UTC
105+
LOG: MultiXact member wraparound protections are now enabled
106+
LOG: autovacuum launcher started
107+
LOG: database system is ready to accept connections
108+
```
109+
69110
# How to extend this image
70111

71112
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.

0 commit comments

Comments
 (0)