DEV Community

Guantai Jp
Guantai Jp

Posted on

How to Install and Set Up PostgreSQL on a Linux Server

Introduction
PostgreSQL is one of the most trusted and widely used open-source relational database systems. It's reliable, powerful, and a great choice whether you're building a small personal project or a production-grade system.

In this guide, I’ll walk you through how to install and set up PostgreSQL on a Linux server. Whether your server is in the cloud, running on a virtual machine, or simply your own laptop, these steps will help you get PostgreSQL up and running.

What Is a Linux Server?

A Linux server is just a computer running a Linux-based operating system, used to host apps, services, or databases like PostgreSQL. It can be:

  • A cloud server (like AWS, DigitalOcean, or Azure)
  • A physical machine (e.g., your laptop running Ubuntu)
  • A virtual machine (VM) using software like VirtualBox or WSL on Windows

No matter the setup, the goal is the same: to get PostgreSQL working as your database engine.

Step 1: Update the Server
Before installing anything, it’s good practice to make sure the system is up to date. Run:

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install PostgreSQL
Now install PostgreSQL and some useful extras:

sudo apt install postgresql postgresql-contrib -y
Enter fullscreen mode Exit fullscreen mode

Once installed, check the version to confirm:

psql --version
Enter fullscreen mode Exit fullscreen mode

Step 3: Start and Enable PostgreSQL
Make sure PostgreSQL is running and will start automatically on reboot:

sudo systemctl start postgresql
sudo systemctl enable postgresql
Enter fullscreen mode Exit fullscreen mode

Check the status with:

sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

Step 4: Log Into PostgreSQL
PostgreSQL comes with a default user called postgres. To use it:

sudo -i -u postgres
psql
Enter fullscreen mode Exit fullscreen mode

You’re now inside the PostgreSQL terminal. To exit:

\q
Enter fullscreen mode Exit fullscreen mode

Step 5: Set a Password for the Default User
Still as the postgres user, set a secure password:

psql
Enter fullscreen mode Exit fullscreen mode

Then run:

ALTER USER postgres WITH PASSWORD 'your_secure_password';
\q
Enter fullscreen mode Exit fullscreen mode

Step 6: Create Your Own Database and User
Let’s create a new database and user that your application can use:

psql
Enter fullscreen mode Exit fullscreen mode
CREATE DATABASE my_database;
CREATE USER my_user WITH ENCRYPTED PASSWORD 'my_password';
GRANT ALL PRIVILEGES ON DATABASE my_database TO my_user;
\q
Enter fullscreen mode Exit fullscreen mode

You can replace my_database and my_user with names that match your project.

Optional: Enable Remote Access
If you plan to connect to PostgreSQL from another machine (like with DBeaver or pgAdmin), you’ll need to allow external connections.

1. Edit PostgreSQL settings:

sudo nano /etc/postgresql/14/main/postgresql.conf
Enter fullscreen mode Exit fullscreen mode

Look for this line:

#listen_addresses = 'localhost'
Enter fullscreen mode Exit fullscreen mode

Change it to:

listen_addresses = '*'
Enter fullscreen mode Exit fullscreen mode

2. Allow remote users:
Edit this file:

sudo nano /etc/postgresql/14/main/pg_hba.conf
Enter fullscreen mode Exit fullscreen mode

Add this at the end:

host    all             all             0.0.0.0/0               md5
Enter fullscreen mode Exit fullscreen mode

3. Restart the PostgreSQL service:

sudo systemctl restart postgresql
Enter fullscreen mode Exit fullscreen mode

Optional: Open Port 5432 (Firewall)
If you have UFW enabled, allow connections to PostgreSQL:

sudo ufw allow 5432/tcp
Enter fullscreen mode Exit fullscreen mode

Optional: Connect Using a GUI (Like DBeaver or pgAdmin)
You can use desktop tools like DBeaver, pgAdmin, or DataGrip to connect to your Linux server.

You’ll need:

  • Host: Your server's IP address
  • Port: 5432
  • Database: my_database
  • User: my_user
  • Password: The one you set earlier

This is helpful if you prefer a visual interface instead of typing SQL in the terminal.

Wrapping Up
That’s it! You now have PostgreSQL installed and configured on your Linux server. You can create databases, connect to them remotely, and start using them in your projects.

Whether you’re using this for a local test setup or a full cloud deployment, PostgreSQL is a great foundation to build on.

Top comments (0)