How to install and configuration PostgreSQL on Ubuntu

How to install and configuration PostgreSQL on Ubuntu

Step 1: Install PostgreSQL

Follow the steps below to install PostgreSQL on your Ubuntu system.


  • Add PostgreSQL Repository:

First, update the package index and install the necessary packages:

sudo apt update
sudo apt install gnupg2 wget

Second, add the PostgreSQL repository:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Third, import the repository signing key:

curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg

Finally, update the package list

sudo apt update

Why need to add PostgreSQL Repository?

Adding the PostgreSQL repository is essential to keep your database up-to-date, secure, and feature-rich. Here's why:

  • Get the Latest Versions: Default system repos often have outdated PostgreSQL versions. Adding the official repo lets you install the newest releases.
  • Access Multiple Versions: Need a specific version? The PostgreSQL repo gives you the flexibility to choose or even run multiple versions side-by-side.
  • Regular Updates & Fixes: Get timely security patches, bug fixes, and performance improvements directly from the PostgreSQL team.
  • Install PostgreSQL:

Install PostgreSQL and its contrib modules:

sudo apt install -y postgresql postgresql-contrib

postgresql-contrib are a collection of extensions, additional functions, tools, and utilities bundled with PostgreSQL.


Great! Now that you've installed a PostgreSQL server on your Ubuntu instance. Next, it's time to configure it for optimal performance and security.


Step 2: PostgreSQL Configuration

  • Enable PostgreSQL to run on startup:

To ensure PostgreSQL runs automatically when your system starts, execute:

sudo systemctl enable postgresql
  • Check PostgreSQL Status:

After installing PostgreSQL, reboot your system and verify its status using:

sudo systemctl status postgresql
Active: active – This confirms that PostgreSQL is currently running.
postgresql.service: enabled – Indicates that the PostgreSQL service is enabled to start automatically during system boot.
preset: enabled – Confirms that the service is configured to adhere to system defaults for startup behavior.

Do you enjoy this blog post?

Step 3: Enhance Security and Customize Configuration

To enhance database security, it's a good practice to change the default port (5432) and the default superuser name (postgres).

Read more