Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

postgresql

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 their any pros/cons.

I check online but didnt get satisfactory answer.

Thanks in advance

like image 788
Nomad Avatar asked Aug 14 '17 09:08

Nomad


People also ask

What is Pg_ctl in postgres?

pg_ctl is a utility for initializing a PostgreSQL database cluster, starting, stopping, or restarting the PostgreSQL database server (postgres), or displaying the status of a running server.

What is postgres service?

Postgres service allows you to provision PostgreSQL databases instances and store your data in them. PostgreSQL is an object-oriented relational database management system to store data securely for retrieval at the request of other software applications.


2 Answers

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

like image 184
Yashas Avatar answered Oct 12 '22 03:10

Yashas


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.

like image 45
Vao Tsun Avatar answered Oct 12 '22 04:10

Vao Tsun