DEV Community

Cover image for How to Install & Set Up PostgreSQL on Linux (Beginner Friendly)-UBUNTU
Mercy Musyoka
Mercy Musyoka

Posted on

How to Install & Set Up PostgreSQL on Linux (Beginner Friendly)-UBUNTU

How to Install PostgreSQL and Set It Up on a Linux Server

PostgreSQL is a powerful, open-source relational database system used by developers, startups, and large enterprises worldwide.

In this guide, I will walk you through installing PostgreSQL on a Linux server and doing a basic setup so you can start using it right away.

Prerequisites

Before you begin, make sure you have:

  • A Linux server (Ubuntu/Debian or CentOS/RHEL)
  • sudo or root access
  • An internet connection
  • Terminal(I used git bash)

๐Ÿ›  Step 1: Access Your Linux Server

First, connect to your Linux server via SSH:
ssh username@server_ip_address

1.Replace the username with server's user(for example in my case mawia)
2.Replace the server_ip_address with your server's ip address

You can as well choose to specify the port after inputing the above( -p 22) but its not a must

๐Ÿ›  Step 2: View Files and Directories

Once connected, you can check the contents of your home directory:
This can be done by;
ls command on the terminal you are using
This helps confirm youโ€™re in the right place before proceeding.

๐Ÿ›  Step 3: Update the System

Itโ€™s best to ensure your system is up-to-date before installing software to avoid vulnerabilities and compatibility issues.
If using Ubuntu, you can use the command;
sudo apt update

๐Ÿ›  Step 4: Install PostgreSQL

The command used in ubuntu is;
sudo apt install postgresql postgresql-contrib -y
After inputing this command you will be asked to choose yes or no please choose yes so as to continue with the process of installation as shown below;

๐Ÿ›  Step 5: Check whether you have PostgreSQL /start

You use the command;
sudo systemctl start PostgreSQL

For example, from the above the state is active

๐Ÿ›  Step 6:Check the version of PostgreSQL

You use the command;
psql --version
For example my version was 16.9 from the screenshot I took below.

๐Ÿ›  Step 7:Now Enable the PostgreSQL

use the command;
sudo systemctl enable postresql
As show below, the PostgreSQL is now active.

๐Ÿ›  Step 8:Check Status

use the command;
sudo systemctl status postgesql
Expect something the this;

๐Ÿ›  Step 9: Now its time to interact with the PostgreSQL

Now that PostgreSQL is installed and running, letโ€™s log in and create our first database.

๐Ÿ›  Switching to PostgreSQL default user

PostgreSQL creates a default Linux user called postgres. Switch to it:
we use the command;
sudo -i -u postresql

  • i- stands for interface
  • u- user
  • postgresql is the default Postresql user


You should see your prompt change to:

postgres@myvm:~$

๐Ÿ›  Access the PostgreSQL Shell

Sinceyouโ€™re the postgres user, open the PostgreSQL interactive terminal using the command;
psql
Example of the result to ecpect is shown below;

๐Ÿ›  Now list the databases created

Using the command,
\l
The default database is postgres as shown below;

๐Ÿ›  Create a new database

Inside the psql shell, now create a new database
using the command;
create database nameofdatabase;
Remember to terminate the query
for example in my case I was creating a database named student.
so i used the command;
create database student

๐Ÿ›  create user

Now create user, with the command;
create user nameofuser with password 'passwordofchoice'
eg in my case;
create user lux with password '1234';
Remember to terminate the query

๐Ÿ› Grant user access to the database user

using the command;
GRANT ALL PRIVILIGES ON DATABASE nameofdatabase TO username
for example in my case,
GRANT ALL PRIVILEGES ON DATABASE STUDENT TO LUX

๐Ÿ›  Now check the databases created

This can be done by using the command;
\l
yow will see the default one which is postgres and the one you just created in my case it was student.

WRAPPING UP!

Congratulations!!!!!!!!!!!!!!!

You have successfully installed and set up PostgreSQL on ubuntu, created your first database, and learned how to view all the available databases with the \l command.

From here, you can connect PostgreSQL to your favorite tool and applications. For example, you might use a postgreSQL GUI tool like pgDdmin or DBeaver, or connect directly from a programming language like python,Node.js or Java.

Here is the general format for a PostgreSQL connecting string:

postgesql://username:password@host:5432/databasename

from the above replace username ,password host and databasename with your own values.

Exiting PostgreSQL and the Terminal

When you're done:

  1. leave the PostgreSQL shell by typing the command; \q
  2. If you are connected to your ubuntu server via SSH, log out by typing the command; exit --- That's it!!!! You are ready now to start building applications backed by a powerful PostgreSQL database. Whether you're developing locally or deploying to production, this setup will give you a reliable foundation for storing and managing your data.

**

QUERY ON BUDDDY!!!!!!!!**

Top comments (0)