How to Start PostgreSQL Automatically on Ubuntu Boot

postgresql show tables

We will guide you through the steps to set up PostgreSQL to start automatically on Ubuntu boot.

PostgreSQL is a popular open-source relational database management system used by many developers and organizations worldwide. In Ubuntu, you can configure PostgreSQL to start automatically upon system boot, ensuring that your database server is always up and running.

Before we begin, make sure you have the following prerequisites in place:

  1. Ubuntu operating system installed on your machine.
  2. PostgreSQL database installed on your Ubuntu system.
  3. Administrative privileges or sudo access to execute commands.

Step 1: Confirm PostgreSQL Installation

First, let’s verify that PostgreSQL is installed on your Ubuntu system. Open a terminal and execute the following command:

$ psql --version

This command will display the version of PostgreSQL installed. If PostgreSQL is not installed, you can install it using the following command:

$ sudo apt update
$ sudo apt install postgresql

Step 2: Enable Auto Start

Ubuntu uses a service management system called systemd to manage services, including PostgreSQL. To enable PostgreSQL to start automatically on boot, we need to create a systemd unit file.

  1. Open a terminal and create a new unit file using the following command:
$ sudo nano /etc/systemd/system/postgresql.service
  1. Add the following content to the file:
Description=PostgreSQL database server

ExecStart=/usr/lib/postgresql/<version>/bin/postgres -D /var/lib/postgresql/<version>/main -c config_file=/etc/postgresql/<version>/main/postgresql.conf
ExecReload=/bin/kill -HUP $MAINPID


Make sure to replace <version> with the specific version of PostgreSQL installed on your system. For example, if you have PostgreSQL 12, the paths will be /usr/lib/postgresql/12/bin/postgres, /var/lib/postgresql/12/main, and /etc/postgresql/12/main/postgresql.conf.

  1. Save the file and exit the text editor.
See also  How to Delete/Drop a Constraint In PostgresSQL

Step 3: Configure Permissions and Reload systemd

Now, we need to set the appropriate permissions on the systemd unit file and reload the systemd daemon.

  1. Set the correct ownership and permissions on the unit file:
$ sudo chown root:root /etc/systemd/system/postgresql.service
$ sudo chmod 644 /etc/systemd/system/postgresql.service
  1. Reload the systemd daemon to load the newly created unit file:
$ sudo systemctl daemon-reload

Step 4: Enable and Start PostgreSQL Service

With the systemd unit file in place, we can now enable and start the PostgreSQL service.

  1. Enable the PostgreSQL service to start automatically on boot:
$ sudo systemctl enable postgresql
  1. Start the PostgreSQL service:
$ sudo systemctl start postgresql
  1. Verify that PostgreSQL is running properly:
$ sudo systemctl status postgresql

The status command should indicate that PostgreSQL is active and running. You can also connect to the database using the psql command to further validate its functionality.

Step 5: Reboot and Test

To ensure that PostgreSQL starts automatically on system boot, it is recommended to reboot your Ubuntu machine:

$ sudo reboot

After the reboot, PostgreSQL should automatically start. To confirm this, follow these steps:

  1. After the reboot, log in to your Ubuntu system.
  2. Open a terminal and execute the following command to check the status of the PostgreSQL service:
$ sudo systemctl status postgresql

The output should indicate that the PostgreSQL service is active and running.

If the connection is successful, you will be greeted with the PostgreSQL command prompt.

You have successfully set up PostgreSQL to start automatically on Ubuntu. From now on, PostgreSQL will automatically start whenever your Ubuntu system boots up, ensuring the availability of your database server.

See also  How to find when PostgreSQL Vacuum and Analyze were run?

Support us & keep this site free of annoying ads.
Shop or Donate with Paypal

Leave a Comment