Install PostgreSQL Linux
Summary: in this tutorial, you will learn how to download and install PostgreSQL on Linux.
Most Linux platforms such as Debian, Red Hat / CentOS, SUSE, and Ubuntu have PostgreSQL integrated with their package management.
It is recommended that you install PostgreSQL this way since it ensures a proper integration with the operating system including automatic patching and other update management functionality.
Install PostgreSQL on Ubuntu
In this tutorial, we’ll show you how to install PostgreSQL 16 on Ubuntu 22.04.
Step 1. Add PostgreSQL Repository
First, update the package index and install the necessary packages:
Second, add the PostgreSQL repository:
Third, import the repository signing key:
Finally, update the package list
Step 2. Install PostgreSQL 16
First, install PostgreSQL and its contrib modules:
Second, start the PostgreSQL service:
Third, enable PostgreSQL service:
Step 3. Configure PostgreSQL server
PostgreSQL stores the configuration in the postgresql.conf
file. You can edit the postgresql.conf
using any text editor such as nano and vim.
Set the listen_addresses to * to allow remote connection:
Configure PostgreSQL to use md5 password authentication in the pg_hba.conf
file. This is necessary if you want to enable remote connections :
Restart PostgreSQL for the changes to take effect:
Allow PostgreSQL port through the firewall:
Connect to the PostgreSQL database server
First, connect to the PostgreSQL server using the postgres
user:
Second, set a password for postgres
user:
Replace the <password>
with the one you want.
Third, quit the psql:
Load the sample database
First, download the sample database using the curl
tool:
Second, unzip the dvdrental.zip file to get the dvdrental.tar file:
Third, connect to the PostgreSQL server using postgres
user:
Fourth, create the dvdrental
database using the CREATE DATABASE
statement:
Fifth, quit the psql
by using the \q
command:
Seventh, use the pg_restore tool to restore the dvdrental
database:
Eighth, access the PostgreSQL database server again using psql
:
Ninth, switch to the dvdental
database:
Finally, enter the following command to get the number of films in the film
table:
Here is the output:
Congratulations! You have successfully installed PostgreSQL on Ubuntu, connected to the PostgreSQL database server using psql, and loaded the sample database.