
PostgreSQL is a reliable and scalable open-source relational database system that supports complex queries, custom data types, and JSON. It's ideal for managing large datasets in applications like analytics, GIS, and dynamic websites. It has a rich feature set perfected over decades and a strong community that ensures resilience and data integrity. Developers prefer it for its flexibility and compatibility with modern data workflows.
This article explains how to install PostgreSQL on a Ubuntu 24.04 server. You will enable the PostgreSQL database server and secure it for production use on your server.
Prerequisites
Before you begin, you need to:
- Have access to an Ubuntu 25.04 instance as a non-root user with sudo privileges.
Install PostgreSQL
PostgreSQL is available in Ubuntu’s default APT package list, so you can install it easily using the terminal. Follow the steps below to set up PostgreSQL, add the official repository, and ensure that the database starts automatically when your server boots. After completing these steps, your server will be ready to use PostgreSQL for storing and managing data.
Update the APT package index.
console$ sudo apt update
Install the
postgresql-common
package, which includes the common utilities and configurations for managing PostgreSQL.console$ sudo apt install -y postgresql-common -y
Run the setup script to add the PostgreSQL APT repository on your server.
console$ sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
When prompted, press the Enter key to set up the repository on your server:
This script will enable the PostgreSQL APT repository on apt.postgresql.org on your system. The distribution codename used will be plucky-pgdg. Press Enter to continue, or Ctrl-C to abort.
Install the PostgreSQL server package.
console$ sudo apt install -y postgresql
Start the PostgreSQL service.
console$ sudo systemctl start postgresql
Enable the PostgreSQL systemd service to start at the time of system boot.
console$ sudo systemctl enable postgresql
Your output should be similar to the one below:
Synchronizing state of postgresql.service with SysV service script with /usr/lib/systemd/systemd-sysv-install. Executing: /usr/lib/systemd/systemd-sysv-install enable postgresql
View the status of the PostgreSQL service to confirm it's in active state.
console$ sudo systemctl status postgresql
Your output should be similar to the one below:
● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) Active: active (exited) since Wed 2025-04-23 13:47:47 UTC; 3min 25s ago Invocation: 40d8e6047ffc4124834b0fb0d1fc16d8 Main PID: 4811 (code=exited, status=0/SUCCESS) Mem peak: 1.6M CPU: 6ms
Secure the PostgreSQL Database Server
PostgreSQL uses the default postgres
superuser account to handle administrative tasks like managing databases and users. On Ubuntu, it defaults to peer authentication, which permits access only to local system users without requiring a password. Follow the steps below to improve security, allow broader access control, enable password authentication and restrict access to only authorized users.
Check the installed PostgreSQL version.
console$ psql --version
Your output should be similar to the one below:
psql (PostgreSQL) 17.4 (Ubuntu 17.4-1.pgdg25.04+2)
Log in to PostgreSQL server as the
postgres
user.console$ sudo -u postgres psql
Set a new strong password for the
postgres
user.psqlpostgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'strong_password';
Replace
strong_password
with a strong password including random characters.Create a new user named
park_admin
and set a strong password for secure authentication.psqlpostgres=# CREATE USER park_admin ENCRYPTED PASSWORD 'strong_password';
Replace
strong_password
with a strong password including random characters.Exit the PostgreSQL console.
psqlpostgres=# EXIT;
Enable password authentication by modifying the
pg_hba.conf
file.console$ sudo sed -i '/^local/s/peer/scram-sha-256/' /etc/postgresql/17/main/pg_hba.conf
Replace
17
with your installed PostgreSQL version if it's different.Restart the PostgreSQL service to apply the changes made to the configuration.
console$ sudo systemctl restart postgresql
View the service status to confirm PostgreSQL service is in active state.
console$ sudo systemctl status postgresql
Your output should be similar to the one below:
● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) Active: active (exited) since Wed 2025-04-23 13:54:24 UTC; 19s ago Invocation: 3bb07d5e081e4289976cf4d957be27a0 Process: 7257 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 7257 (code=exited, status=0/SUCCESS) Mem peak: 1.6M CPU: 6ms
Access the PostgreSQL Database Server
You can access the PostgreSQL database console through the pre-installed psql utility, which comes with the server package. Alternatively, you can use graphical tools that establish a direct connection to the server. Follow the steps below to access the PostgreSQL database console and create a new sample database for use with a non-privileged user.
Create a new sample PostgreSQL database
amusement_park
and grant the ownership to thepark_admin
user.console$ sudo -u postgres createdb amusement_park -O park_admin
When prompted, enter the
postgres
user password you created earlier.Log in to the PostgreSQL database as the
park_admin
user.console$ sudo -u postgres psql -U park_admin -d amusement_park
When prompted, Enter the
park_admin
user password and press Enter to access the database.Create a new table
visitors
in theamusement_park
database.psqlamusement_park=> CREATE TABLE visitors ( visitor_id SERIAL PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), entry_date DATE );
The above SQL statement creates a new table in the
amusement_park
database with the following columns:visitor_id
is aPRIMARY KEY
that uniquely identifies each visitor.SERIAL
generates a newvisitor_id
for each new record.first_name
andlast_name
store the visitor’s name.entry_date
stores the date the visitor entered the amusement park.
Insert sample data into the
visitors
table.psqlamusement_park=> INSERT INTO visitors ( first_name, last_name, entry_date) VALUES ( 'Alice', 'Wong', '2024-11-15'), ( 'Brian', 'Taylor', '2024-02-28'), ( 'Clara', 'Nguyen', '2024-04-10');
Query the
visitors
table to view all available records.psqlamusement_park=> SELECT * FROM visitors;
Your output should be similar to the one below:
visitor_id | first_name | last_name | entry_date ------------+------------+-----------+------------ 1 | Alice | Wong | 2024-11-15 2 | Brian | Taylor | 2024-02-28 3 | Clara | Nguyen | 2024-04-10 (3 rows)
Exit the PostgreSQL console.
psqlamusement_park=> EXIT;
Conclusion
You have installed PostgreSQL on your Ubuntu 25.04 server and used the psql
utility to create databases and manage records. You can now integrate PostgreSQL with your applications to securely manage data. For additional information and configuration options, refer to the official PostgreSQL documentation.
No comments yet.