Closed
Description
I want to restart postgres server to load new configuration in /var/lib/postgresql/data/postgresql.conf but failed to run: "/etc/init.d/postgresql restart" or "pg_ctl restart" (got error message: 'pg_ctl: cannot be run as root Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.')
Please help.
Activity
yosifkit commentedon Nov 10, 2016
Since there is no init system running in the container, the only process running in the container is postgres itself, so
docker restart container-name
ordocker kill -sTERM container-name
followed bydocker start container-name
will work.As long as you are not running the process with
docker run -it --rm ...
, or doingdocker rm
after you stop it, then the container's data will stick around and you can start it up again.RichardJECooke commentedon Feb 14, 2017
What if we want to do something in the container while it's running? Like for instance move Postgresql into /dev/shm so tests run superfast in RAM. You need to stop and restart Postgresql while the container is up for that or the RAM disk gets deleted.
RichardJECooke commentedon Feb 14, 2017
I thought maybe I could try
But my container exits immediately after running the first line
Rajashreegr commentedon Jun 14, 2017
I tried to restart the Postgres while it had one active connection. Now I'm not able to start it again. Is there anyway I can start it? I don't want to loose any data :(
RichardJECooke commentedon Jun 14, 2017
I wrote up how to do this extensively a while back, it might help:
https://richardcooke.info/how-to-run-your-tests-against-an-in-memory-database-with-a-rails-rspec-mysql-example/
and
https://stackoverflow.com/questions/42226418/how-to-move-postresql-to-ram-disk-in-docker
isharov commentedon Sep 29, 2017
You can run postgres in background (by supervisor for example), or set restart policy in compose file.
tianon commentedon Apr 24, 2018
For the ramdisk use-case, I usually start another container to keep the ramdisk around appropriately, ala:
When starting PostgreSQL:
$ docker run ... -v my-new-volume:/var/lib/...
When I want to restart PostgreSQL or re-create the container:
Since the original question is answered, I'm going to close.
SeWieland commentedon Aug 10, 2018
As is stumbled upon the same problem a few minutes ago:
su - postgres -c "PGDATA=$PGDATA /usr/lib/postgresql/10/bin/pg_ctl -w restart"
in case of version 10, worked for me ;) 👍
Dean-Christian-Armada commentedon Aug 15, 2018
@SeWieland , your command will restart the container. It's not as smooth as nginx's
nginx -s reload
.Command below is the perfect command to avoid restart and most importantly does not disrupt ongoing queries
SeWieland commentedon Aug 15, 2018
@Dean-Christian-Armada thank you! That was exactly what I searched for.
And it is actually the accurate answer to the original question.
Budgiebrain994 commentedon Jan 3, 2019
Calling
pg_reload_conf()
will only reload parameters which don't require a restart to update. [Source]For this reason, a restart remains a viable option for cases where configuration options can't be reloaded.
Dean-Christian-Armada commentedon Jan 5, 2019
But still, this proves to be useful on some configurations rather than restarting a container and having downtime
Luokun2016 commentedon Apr 28, 2020
login with postgres user, and
execute "SELECT pg_reload_conf()".
chance2021 commentedon Apr 5, 2022
To reload the config, you can login with postgres user and run the reload command