How to Install PostgreSQL on Ubuntu 24.10
PostgreSQL is a powerful, open-source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.
Install PostgreSQL
Install PostgreSQL on Ubuntu by using the following command,
apt update
apt install postgresql postgresql-contrib
Output:
root@server:~# apt install postgresql postgresql-contrib
Installing:
postgresql postgresql-contrib
Installing dependencies:
libcommon-sense-perl libjson-xs-perl libpq5 postgresql-16 postgresql-client-common ssl-cert
libjson-perl libllvm19 libtypes-serialiser-perl postgresql-client-16 postgresql-common
Suggested packages:
postgresql-doc postgresql-doc-16
Summary:
Upgrading: 0, Installing: 13, Removing: 0, Not Upgrading: 0
Download size: 45.6 MB
Space needed: 180 MB / 44.9 GB available
Continue? [Y/n]
PostgreSQL Roles and Databases
We'll switch to the Postgres user account for the next steps, to switch to the Postgres account, use the following command,
sudo -i -u postgres
You can access a PostgreSQL prompt using the psql utility,
psql
Output:
postgres@server:~$ psql
psql (16.4 (Ubuntu 16.4-1build1))
Type "help" for help.
postgres=#
To exit the postgres shell, use the below command,
\q
Create PostgreSQL Role
Now let us see how we can create additional users that can interact with the Databases.
For this, you have to be a postgres user and then run the command as shown below,
createuser --interactive
Output:
postgres@server:~$ createuser --interactive
Enter name of role to add: adam
Shall the new role be a superuser? (y/n) y
Create a PostgreSQL Database
Creating a database is as simple as it gets. Run the below command as a Postgres user account,
createdb database_name
Example:
postgres@server:~$ createdb my_db
Open a Postgres Prompt with the New Role
For this, we will create a new Linux system user by using adduser
.
For simplicity purposes, we will be using the same name as which we created the Postgres role, adam.
You will need to switch back to root user or a sudo user with the required privileges .
adduser username
Example:
root@server:~# adduser adam
Adding user `adam' ...
Adding new group `adam' (1000) ...
Adding new user `adam' (1000) with group `adam' ...
Creating home directory `/home/adam' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
...
Switch over to the newly added user and connect to the database.
su username
Open the postgres shell and connect to the new Database,
psql -d database_name
Example:
root@server:~# su adam
adam@server:/root$ psql -d my_db
psql (16.4 (Ubuntu 16.4-1build1))
Type "help" for help.
my_db=#
Once you're logged in as adam, check your current connection information:
\conninfo
Output:
client_db=# \conninfo
You are connected to database "client_db" as user "client" via socket in "/var/run/postgresql" at port "5332".
client_db=#
This concludes the topic of installing PostgreSQL on Ubuntu 24.10.
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