Reputation: 1139
I would like to know correct way of starting/stopping postgres database. There are two ways
pg_ctl start/stop
service postgresql start/stop
I would like to know how they are different from each other, which one should I use? Are there any pros/cons?
Upvotes: 32
Views: 95740
Reputation: 111
The correct answer is: you should stop it how you started it.
The service
command is used to control System V services, so if you control PostgreSQL via a System V service (for example to auto (re)start the PostgreSQL daemon), then you should stop/start it with the service
command.
The pg_ctl
command is a native PostgreSQL command to control the server. You specify the server by passing the location of the data directory the server is running from. Under the hood, the pg_ctl
command obtains the PID of the server process via the PID file in the data directory and then sends the appropriate signals to the process (source, search for SHUTDOWN_MODE):
pg_ctl stop -m s[mart] --> SIGTERM
pg_ctl stop -m f[ast] --> SIGINT
pg_ctl stop -m i[mmediate] --> SIGQUIT
So, if for whatever reason pg_ctl is not working (PID file not obtainable), you can just send the appropriate signal to the PostgreSQL server process.
Upvotes: 1
Reputation: 28502
On macOS, I've found that running the following will stop and start the server
cd /
to avoid permission errors, e.g. could not identify current directory: Permission denied
sudo -u postgres pg_ctl -D /Library/PostgreSQL/15/data start
sudo -u postgres pg_ctl -D /Library/PostgreSQL/15/data stop
15
to the real path of your Postgres.Upvotes: 3
Reputation: 121
On Linux I do the following
to start:
systemctl start postgresql
to stop:
systemctl stop postgresql
Upvotes: 8
Reputation: 367
To start:
brew services start postgresql
To stop:
brew services stop postgresql
To start:
pg_ctl start -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log
To stop:
pg_ctl stop -D /usr/local/var/postgres -m smart
See: https://www.postgresql.org/docs/current/app-pg-ctl.html
Upvotes: 36
Reputation: 677
cd /datadir
pg_ctl -D $(pwd) stop
pg_ctl -D $(pwd) start
#pg_ctl needs the path of the data (-D)
Can be directly used:
pg_ctl -D /mnt/raid/pdb stop
pg_ctl -D /mnt/raid/pdb stop
Upvotes: 1
Reputation: 51456
If you view /etc/init.d/postgres${VER}
file, you will find out, that when you run service postgresql start/stop
it runs pg_ctl $OPTIONS start/stop
. The only difference is that service gives you handy way to store environmental variables and enable/disable autostart.
All above can be done manually, using pg_ctl
and some scripting.
Upvotes: 7