Skip to content

Document where to put postgres.conf file #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yajo opened this issue Feb 28, 2017 · 13 comments · Fixed by docker-library/docs#1095
Closed

Document where to put postgres.conf file #266

yajo opened this issue Feb 28, 2017 · 13 comments · Fixed by docker-library/docs#1095

Comments

@yajo
Copy link

yajo commented Feb 28, 2017

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.

@dfang
Copy link

dfang commented Mar 15, 2017

+1

1 similar comment
@onuryavuz
Copy link

+1

@yosifkit
Copy link
Member

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"]
$ # 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:

$ 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

@alexanderkjeldaas
Copy link

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

@ghost
Copy link

ghost commented Sep 25, 2018

@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?

@dhinus
Copy link

dhinus commented Sep 25, 2018

@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

@ghost
Copy link

ghost commented Sep 25, 2018

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

@spacepirate0001
Copy link

spacepirate0001 commented Feb 21, 2019

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

@dhinus
Copy link

dhinus commented Feb 21, 2019

@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'

@vesnikos
Copy link

@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'

@dhinus
Copy link

dhinus commented Jul 31, 2019

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

@germansokolov13
Copy link

germansokolov13 commented Oct 21, 2019

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.

@janhartigan
Copy link

@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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants