Nomad
Nomad

Reputation: 1139

correct way to start/stop postgres database pg_ctl or service postgres

I would like to know correct way of starting/stopping postgres database. There are two ways

  1. pg_ctl start/stop
  2. 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

Answers (6)

Flourid
Flourid

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

Ben Butterworth
Ben Butterworth

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
  • Start: sudo -u postgres pg_ctl -D /Library/PostgreSQL/15/data start
  • Stop: sudo -u postgres pg_ctl -D /Library/PostgreSQL/15/data stop
  • Remember to update 15 to the real path of your Postgres.

Upvotes: 3

Konstantinos
Konstantinos

Reputation: 121

On Linux I do the following

to start:

systemctl start postgresql

to stop:

systemctl stop postgresql

Upvotes: 8

Yashas
Yashas

Reputation: 367

If you are using Mac with brew

To start:

brew services start postgresql

To stop:

brew services stop postgresql

Using pg_ctl

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

Chinmaya Biswal
Chinmaya Biswal

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

Ref

Upvotes: 1

Vao Tsun
Vao Tsun

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

Related Questions