PostgreSQL – Command Not Found: pg_ctl on Ubuntu

debianpostgresqlUbuntu

PostgreSQL 9.4.8 Ubuntu 16.04 64 bit

I just want an explanation of what is happening here. Why is this command not found? Nothing else.

malikarumi@Tetuoan2:/usr/lib/postgresql/9.4/bin$ pg_ctl stop -m fast
pg_ctl: command not found
malikarumi@Tetuoan2:/usr/lib/postgresql/9.4/bin$ sudo pg_ctl stop -m fast
[sudo] password for malikarumi: 
sudo: pg_ctl: command not found
malikarumi@Tetuoan2:/usr/lib/postgresql/9.4/bin$ ls
clusterdb   initdb             pg_ctl          pg_restore      postmaster
createdb    oid2name           pg_dump         pg_standby      psql
createlang  pg_archivecleanup  pg_dumpall      pg_test_fsync   reindexdb
createuser  pg_basebackup      pg_isready      pg_test_timing  vacuumdb
dropdb      pgbench            pg_receivexlog  pg_upgrade      vacuumlo
droplang    pg_config          pg_recvlogical  pg_xlogdump
dropuser    pg_controldata     pg_resetxlog    postgres
malikarumi@Tetuoan2:/usr/lib/postgresql/9.4/bin$

Best Answer

pg_ctl by design isn't in the path of the version of PostgreSQL Debian (and therefore Ubuntu) distributes. PostgreSQL is installed as a service in Debian/Ubuntu

sudo service postgresql {start|stop|restart|reload|force-reload|status} [version ..]

That command calls an init.d script which wraps around pg_ctlcluster. You can see that by opening /etc/init.d/postgresql. You can call pg_ctlcluster if you must. pg_ctrlcluster is a perl script that wraps around pg_ctl which isn't exposed (in the path) by design.

pg_ctrlcluster: multiversion/cluster aware pg_ctl wrapper; this also supplies the correct configuration parameters to 'start', and makes sure that postgres really stops on 'stop'.

pg_ctl is technically distributed, but there is no reason to ever call it directly. To find it run

find /usr/lib/postgresql/ -name pg_ctl

You can call pg_ctl explicitly with the full path, however don't.

Also, going above and beyond and teaching you how to fish, you can always use apropos if you have these kind of questions later.

$ apropos pg_ctl
pg_ctl (1)           - initialize, start, stop, or control a PostgreSQL server
pg_ctlcluster (1)    - start/stop/restart/reload a PostgreSQL cluster