How do you nicely stop all postgres processes with pg_ctl
(or otherwise), when you don't recall what the database directory is, nor have the PGDATA environment variable defined?
How do you nicely stop all postgres processes with pg_ctl
(or otherwise), when you don't recall what the database directory is, nor have the PGDATA environment variable defined?
It's safe to:
That kills all processes running as user
postgres
. Or:That kills all processes named 'postgres'.
Do not use
kill -9
(kill -KILL
). Justkill
(without options) does aSIGTERM
, which is what you want.Alternatively, you can check the pgdata location if you can connect to PostgreSQL. For example:
...or by checking its environment variables in
/proc/[postmaster pid]/environ
, where you identify the postmaster withps -fHC postgres
. Look for the one that's the parent of the otherpostgres
processes. For example:Its datadir will generally be shown on its command line.
It makes me nervous seeing kill and postgres in the same command. To answer the question using only
pg_ctl
, that would be:The -X argument says to ignore the
.psqlrc
file. This is useful if you have psql configured to emit the time taken by a query (via the \timing command).The -t argument says to remove the column name at the top of the output and the total number of rows produced.
The -c argument contains the SQL code to be executed.
Running a bare
psql -c 'show data_directory'
will probably produce the following output:Hence, backticking this through
$( ... )
will deliver/path/to/postgresql/data
to the -D argument of pg_ctl, which will then stop the database in an orderly manner.This work for me ref. https://stackoverflow.com/a/5408501/248616
You can simply run the command
When you wish to restart it run
sudo