Select Page

Introduction

Servers often require to be stopped for maintenance, and started again as soon as it’s done. To do this, you’d probably require some command in the terminal through which you’re managing the server. If your server of choice is PostgreSQL, then the command you’d need to start and stop the server is called pg_ctl. This command is responsible for starting and stopping your PostgreSQL servers on demand.

Unfortunately, there are users who have faced the following error in their terminal when trying to use the command:

$ pg_ctl: command not found

or maybe even something like the one below (when using ZSH Shell):

$ zsh: command not found: pg_ctl

If this is the exact situation you yourself are facing, then this article will guide you through the fix to it and help you manage your PostgreSQL server without a hitch!


Why is the pg_ctl command not found?

Unlike most other appearances of the command not found” errors in Linux, this one is a bit more annoying. Simply installing the command won’t work, as the command does not have a package for itself, but rather, comes shipped as a sub-command with other packages.

The problem is, installing the package required for pg_ctl alone won’t get the job done, as you’d probably need to fix your PATH variable by including the directory where the command is located, which is also not a good thing to do. Because the command, by design, is not meant to be executed manually. In the following sections, we’ll discuss these problems and their solutions in detail. 


Install PostgreSQL for pg_ctl

First ensure that you have the  PostgreSQL properly installed in your Linux machine. If you don’t, you need to do so by using your package manager. The commands required to install the PostgreSQL package in the most commonly used Linux distributions are listed below:

 Arch Linux

$ sudo pacman -Syu
$ sudo pacman -S postgresql

Ubuntu

$ sudo apt-get update
$ sudo apt-get install postgresql

Debian

$ sudo apt-get update
$ sudo apt-get install postgresql

Fedora

$ sudo yum makecache
$ sudo yum install postgresql

RedHat

$ sudo dnf makecache
$ sudo dnf install postgresql

CentOS

$ sudo dnf makecache
$ sudo dnf install postgresql

The first line of command is required to update your system repositories, as otherwise you’d be unable to install any packages sometimes. The second line of command installs the postgresql package on your Linux machine. If everything goes well, you should now be able to use the pg_ctl command. However, this alone might not be the solution for many users out there, because there’s a little more that you’re left to do. 


Fix the PATH Variable

Besides installing postgreSQL, you need to include the path to where the pg_ctl command is located in your system’s global PATH variable. If you’re using Ubuntu or Debian, this problem is more relevant as this is usually a requirement for these specific distributions, and their forks. 

In order to achieve this, you first need to figure out which version of PostgreSQL you’re using. You can use a command for this, but let us use the generic method.

Use the following command to enter the postgreSQL installation directory:

$ cd /usr/lib/postgresql/

Inside this directory, use the ls command:

$ ls

This should return you the version of postgresql which is installed. Now take note of this version. For example, let’s say that the version is 9.3 (which is the most common one), so we’ll take note of it for use in the next command.

What we need to do next is use a command called export, but not directory, but rather including it inside a shell startup script. This will ensure that whenever you start your terminal in search of executing the pg_ctl command, you can do so without needing to use the export command again and again every time.

Inside your home directory, there should be a file called .bashrc, or it could be .zshrc (based on the shell your terminal is using). Whichever file you find, enter that file using your text manager of choice, and append the following line of code in it:

export PATH=$PATH:/usr/lib/postgresql/9.3/bin/

Once done, save and exit the file, and restart your terminal for changes to take effect. You should now properly be able to execute the pg_ctl command in your terminal.


Use pg_ctlcluster command instead

In case you’re still unable to run the pg_ctl command, there is still hope for you. Because there’s an alternative command to pg_ctl, which is called pg_ctlcluster. This command functions the same as pg_ctl, but has a few other functionalities under the hood. In other words, it is essentially just a wrapper for the pg_ctl command. But the only problem is that it’s not as straightforward and simple as the pg_ctl command. You need to pass two more values along with the command in order for it to work.

Thanks to the guy called Deeplogger in Stackoverflow, he provided an easy and quick solution to finding your cluster name and version. According to his post, in order to figure out for these valued, you’d look out for the following directory:

/etc/postgresql/<cluster-version>/<cluster-name>/start.conf

Simply enter the /etc/postgresql/ directory and use the ls command to find the cluster version, and make note of it. After that, simply enter that directory and use ls again to find the cluster name.

Once you have noted these two valued, simply use them in the following command:

$ sudo pg_ctlcluster <cluster_version> <cluster-name> <action>

Replace the <action> part with any of the followings, based on what you want to do:

  • start
  • stop
  • restart
  • reload
  • status
  • promote

This way you can use the pg_ctlcluster command as an alternative if you are unable to use the pg_ctl command in your Linux machine!