DEV Community

Dilan Bosire
Dilan Bosire

Posted on

How to Install and Set Up PostgreSQL on a Linux Server

In this article, I will show you how to install and set up PostgreSQL on a Linux server.

Make sure you have:

  • A Linux server

  • Internet access to install packages

Step 1: Log into your Ubuntu server

you can use the command,
ssh username@server_ip_
Then you'll be prompted to enter your password.
If the password entered is correct then you are in the server.

Step 2: Install PostgreSQL

Search https://www.postgresql.org/download/ to see the download versions of available.
Select the one for linux and ubuntu.
You'll get the image below.

Copy and paste the lines of code as below into your ubuntu server, and you'll have downloaded posgresql.

sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
. /etc/os-release
sudo sh -c "echo 'deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
sudo apt update
sudo apt -y install postgresql-16
Enter fullscreen mode Exit fullscreen mode

Step 3: Connect to PostgreSQL

PostgreSQL creates a system user named postgres. You can switch to this user to open the PostgreSQL prompt:
We use,
sudo -i -u postgres

Then type, psql.
You should now be at the PostgreSQL prompt:

postgres=#

To exit the prompt, type:
\q

Then return to your regular user:

exit

Step 4: Create a New Database and User

If you want to create a new PostgreSQL user and database:

Switch to the postgres user:
sudo -i -u postgres

Create a new user:
createuser --user_name

It will prompt you for a username and whether the new user should be a superuser.

Create a new database:
createdb database_name

Now you can select the database you want to use and add tables and values to it, to select use
\c database_name
Then from there you can add your tables and values.

Check posgres status:
sudo systemctl status posgresql

To stop posgresql:
sudo systemctl stop posgresql

To start posgresql:
sudo systemctl start posgresql

To restart posgresql:
sudo systemctl restart posgresql

Top comments (0)