DEV Community

Cover image for Installing PostgreSQL on a Linux Server: An Interactive Beginner-Friendly Guide
musyimi254
musyimi254

Posted on

Installing PostgreSQL on a Linux Server: An Interactive Beginner-Friendly Guide

Introduction

Let's Get Our Hands Dirty!
Hey there, Linux adventurer!
Thinking of installing PostgreSQL on your server?
Well, you're in the right place and don't worry, we're not just going to throw terminal commands at you and disappear.

This guide is interactive you’ll read, you’ll type, you’ll feel powerful.

What is PostgreSQL anyway?

PostgreSQL (or Postgres) is a free, open-source, object-relational database that powers web apps, enterprise systems, and even scientific research. In short if you’ve got data, Postgres can handle it.

So before we jump in, ask yourself:

  1. Do you have terminal access (with sudo)?
  2. Are you working on a fresh Linux install or an existing server?
  3. Do you know how to copy paste without breaking things?

If your answers are mostly “yes,” then high five!
Let’s get your Linux box speaking fluent PostgreSQL.

“The best way to learn is to do. So open that terminal we’ve got some commands to run.”

  1. System Update Command: 1. System UpdaCommand: sudo apt update && sudo apt upgrade -y This command updates your system package list and installs the latest versions of all packages. It ensures your system is ready for PostgreSQL.

bash
Copy
Edit

  1. Install PostgreSQL
    Command: sudo apt install postgresql postgresql-contrib -y

  2. Switch to PostgreSQL User and Access Shell
    Command: sudo -i -u postgres → psql

  3. Set PostgreSQL Password
    Command inside psql: \password postgres

  4. Create Database and User
    SQL Commands:

sql
Copy
Edit
CREATE DATABASE testdb;
CREATE USER testuser WITH ENCRYPTED PASSWORD 'yourpassword';
GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;

  1. Edit Config for Remote Access Command: sudo nano /etc/postgresql/14/main/postgresql.conf

Command: sudo nano /etc/postgresql/14/main/pg_hba.conf

  1. Restart and Check Status Command: sudo systemctl status postgresql

Conclusion

You’ve now successfully installed and set up PostgreSQL on your Linux server. With just a few commands, your system is ready to store, manage, and query data efficiently. PostgreSQL is powerful, secure, and ideal for both small projects and large applications. Keep exploring its features to get the most out of your database setup.

Top comments (0)