Postgres: Document where to put postgres.conf file

Created on 28 Feb 2017  路  13Comments  路  Source: docker-library/postgres

Right now there's no clue in docs on how to configure Postgres.

Although it would be nice to have an environment variable to apply configuration, at least we should have properly documented where postgres.conf file should be located.

People gets confused with this.

Most helpful comment

So add something like the following to the docs on the Docker Hub?

$ docker run -d -v /my/pg.conf:/usr/share/postgresql/postgresql.conf.sample postgres

The biggest problem with this is that after first start, this file not will be used again and any changes would not be reflected in restarts of the container.

I think the workarounds in the linked stack-overflow issue seem pretty solid. -c arguments are my preferred way for simple adjustments to config: docker run -d postgres -c shared_buffers=256MB.

You can even just tell postgres where the config file is located and build an image with the new config built in:

FROM postgres:9.6
COPY pg.conf /etc/postgresql/postgresql.conf
CMD ["-c", "config_file=/etc/postgresql/postgresql.conf"]

```console
$ # after building:
$ docker run -d \
-v ./path/to/pg-data/:/var/lib/postgresql/data/ \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=s3cret \
--name postgres \
custom-postgres

Or you can bind-mount in a config and do it all on the docker cli:

```console
$ docker run -d \
    -v ./path/to/postgres-conf/:/etc/postgresql/ \
    -v ./path/to/pg-data/:/var/lib/postgresql/data/ \
    -e POSTGRES_USER=postgres \
    -e POSTGRES_PASSWORD=s3cret \
    --name postgres \
    postgres:9.6 -c config_file=/etc/postgresql/postgresql.conf

All 13 comments

+1

+1

So add something like the following to the docs on the Docker Hub?

$ docker run -d -v /my/pg.conf:/usr/share/postgresql/postgresql.conf.sample postgres

The biggest problem with this is that after first start, this file not will be used again and any changes would not be reflected in restarts of the container.

I think the workarounds in the linked stack-overflow issue seem pretty solid. -c arguments are my preferred way for simple adjustments to config: docker run -d postgres -c shared_buffers=256MB.

You can even just tell postgres where the config file is located and build an image with the new config built in:

FROM postgres:9.6
COPY pg.conf /etc/postgresql/postgresql.conf
CMD ["-c", "config_file=/etc/postgresql/postgresql.conf"]

```console
$ # after building:
$ docker run -d \
-v ./path/to/pg-data/:/var/lib/postgresql/data/ \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=s3cret \
--name postgres \
custom-postgres

Or you can bind-mount in a config and do it all on the docker cli:

```console
$ docker run -d \
    -v ./path/to/postgres-conf/:/etc/postgresql/ \
    -v ./path/to/pg-data/:/var/lib/postgresql/data/ \
    -e POSTGRES_USER=postgres \
    -e POSTGRES_PASSWORD=s3cret \
    --name postgres \
    postgres:9.6 -c config_file=/etc/postgresql/postgresql.conf

In 17.06 it should be possible to do this using config objects.

@yosifkit
I tried the following and it works perfectly fine.

You can even just tell postgres where the config file is located and build an image with the new config built in:

FROM postgres:9.6
COPY pg.conf /etc/postgresql/postgresql.conf
CMD ["-c", "config_file=/etc/postgresql/postgresql.conf"]

But I am having a hard time understanding why this CMD is not overriding the CMD in the parent image.
The parent image contains this:

ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]

and the docker-entrypoint.sh just does exec $@ at the end.
Can you please explain?

@iabtyagi the magic happens here: if CMD starts with a -, postgres is prepended.

There are a couple of interesting Bash features here:

  • ${1:0:1} returns the first char of $1 [1]
  • set -- postgres $@ reassigns $1, $2, etc. to postgres, followed by the original arguments ($@) [2]

[1] https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
[2] https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

@dhinus Great! Thanks. Totally missed that part of the script. Thanks for explaining the ${1:0:1} and set, I wasn't aware of these features.

im still not sure how to add the custom config file! @dhinus can you share an example?

@Haythamamin you can find an example at https://hub.docker.com/_/postgres#database-configuration

$ # run postgres with custom config
$ docker run -d --name some-postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf postgres -c 'config_file=/etc/postgresql/postgresql.conf'

@Haythamamin you can find an example at https://hub.docker.com/_/postgres#database-configuration

$ # run postgres with custom config
$ docker run -d --name some-postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf postgres

@dhinus you missed a part of the example:
docker run -d --name some-postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf postgres -c 'config_file=/etc/postgresql/postgresql.conf'

@vesnikos you're right, thanks. I have updated my previous comment!

I tried adding -c config_file=/etc/postgresql/postgresql.conf to CMD in the Dockerfile. It changed config file successfuly. However remote access to container db no longer works.
I used postgres:11.2-alpine.

@germansokolov13 make sure your mounted config file has this option:

listen_addresses = '*'

That's there in the default config file that gets created by postgres, but it may not be there in yours. Here's the full list of things that are uncommented in the default config file:

listen_addresses = '*'
max_connections = 100
shared_buffers = 128MB
dynamic_shared_memory_type = posix
max_wal_size = 1GB
min_wal_size = 80MB
log_timezone = 'Etc/UTC'
datestyle = 'iso, mdy'
timezone = 'Etc/UTC'
lc_messages = 'en_US.utf8'
lc_monetary = 'en_US.utf8'
lc_numeric = 'en_US.utf8'
lc_time = 'en_US.utf8'
default_text_search_config = 'pg_catalog.english'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

phanikumarp picture phanikumarp  路  3Comments

orion110217 picture orion110217  路  3Comments

tjamet picture tjamet  路  4Comments

AlpNek picture AlpNek  路  3Comments

harrybvp picture harrybvp  路  4Comments