How to Install PostgreSQL on Ubuntu 25.04

PostgreSQL is a powerful, open-source object-relational database system with over 30 years of active development, earning a strong reputation for reliability, feature robustness, and performance.

Install PostgreSQL

Update your package list and install PostgreSQL using the following commands:

apt update
apt install postgresql postgresql-contrib

Output:

root@server:~# apt update
apt install postgresql postgresql-contrib
Hit:1 http://nl.archive.ubuntu.com/ubuntu plucky InRelease
Hit:2 http://nl.archive.ubuntu.com/ubuntu plucky-updates InRelease
Hit:3 http://nl.archive.ubuntu.com/ubuntu plucky-backports InRelease
Hit:4 https://download.docker.com/linux/ubuntu plucky InRelease
Hit:5 http://security.ubuntu.com/ubuntu plucky-security InRelease
All packages are up to date.
Note, selecting 'postgresql' instead of 'postgresql-contrib'
Installing:
  postgresql

Installing dependencies:
  libcommon-sense-perl  libllvm20                 postgresql-client-common
  libio-pty-perl        libpq5                    postgresql-common
  libipc-run-perl       libtypes-serialiser-perl  postgresql-common-dev
  libjson-perl          postgresql-17             ssl-cert
  libjson-xs-perl       postgresql-client-17

Suggested packages:
  postgresql-doc  postgresql-doc-17

Summary:
  Upgrading: 0, Installing: 15, Removing: 0, Not Upgrading: 0
  Download size: 49.2 MB
  Space needed: 200 MB / 5,451 MB available

PostgreSQL Roles and Databases

We'll now switch to the postgres user account. Run:

sudo -i -u postgres

Then access the PostgreSQL prompt using psql:

psql

Output:

postgres@server:~$ psql
psql (17.4 (Ubuntu 17.4-1))
Type "help" for help.

postgres=#

To exit the PostgreSQL prompt, type:

\q

Create a PostgreSQL Role

To create a new PostgreSQL user (role), run:

createuser --interactive

Output:

postgres@ubuntu25:~$ createuser --interactive
Enter name of role to add: adam
Shall the new role be a superuser? (y/n) y

Create a PostgreSQL Database

Create a new database as the postgres user:

createdb database_name

Example:

postgres@ubuntu25:~$ createdb my_db

Open a Postgres Prompt with the New Role

Now, create a Linux system user with the same name as the PostgreSQL role (for easier access):

adduser username

Example:

root@server:~# adduser adam
info: Adding user `adam' ...
info: Selecting UID/GID from range 1000 to 59999 ...
info: Adding new group `adam' (1001) ...
info: Adding new user `adam' (1001) with group `adam (1001)' ...
info: Creating home directory `/home/adam' ...
info: Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for adam
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []: 1
        Work Phone []: 456789
        Home Phone []: 987654
        Other []:
Is the information correct? [Y/n] y
info: Adding new user `adam' to supplemental / extra groups `users' ...
info: Adding user `adam' to group `users' ...

Switch to the new Linux user:

su username

Example:

su adam

Then connect to the database:

psql -d database_name

Example:

psql -d my_db

Once connected, you can check your connection info using:

\conninfo

Output:

my_db=# \conninfo
You are connected to database "my_db" as user "adam" via socket in "/var/run/postgresql" at port "5432".

That concludes installing and basic setup of PostgreSQL on Ubuntu 25.04!


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS