0
0
PostgresqlHow-ToBeginner · 3 min read

How to Install PostgreSQL on Linux: Step-by-Step Guide

To install PostgreSQL on Linux, use your distribution's package manager like apt for Ubuntu or yum for CentOS. Run sudo apt install postgresql on Ubuntu or sudo yum install postgresql-server on CentOS, then start the service with sudo systemctl start postgresql.
📐

Syntax

Use your Linux distribution's package manager to install PostgreSQL. The commands differ by distribution:

  • Ubuntu/Debian: Use apt to install and manage packages.
  • CentOS/RHEL: Use yum or dnf for package management.
  • After installation, use systemctl to start and enable the PostgreSQL service.
bash
sudo apt update
sudo apt install postgresql
sudo systemctl start postgresql
sudo systemctl enable postgresql
💻

Example

This example shows how to install PostgreSQL on Ubuntu Linux, start the service, and check its status.

bash
sudo apt update
sudo apt install postgresql
sudo systemctl start postgresql
sudo systemctl status postgresql
Output
● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2024-06-20 10:00:00 UTC; 5s ago Main PID: 1234 (postgres) Tasks: 5 (limit: 4915) Memory: 20.0M CGroup: /system.slice/postgresql.service
⚠️

Common Pitfalls

Common mistakes when installing PostgreSQL on Linux include:

  • Not updating package lists before installation, causing outdated packages.
  • Forgetting to start or enable the PostgreSQL service after installation.
  • Using the wrong package name for your Linux distribution.
  • Not checking firewall settings that may block PostgreSQL connections.
bash
Wrong (Ubuntu without update):
sudo apt install postgresql

Right:
sudo apt update
sudo apt install postgresql
sudo systemctl start postgresql
📊

Quick Reference

CommandDescription
sudo apt updateRefresh package list on Ubuntu/Debian
sudo apt install postgresqlInstall PostgreSQL on Ubuntu/Debian
sudo yum install postgresql-serverInstall PostgreSQL on CentOS/RHEL
sudo systemctl start postgresqlStart PostgreSQL service
sudo systemctl enable postgresqlEnable PostgreSQL to start on boot
sudo systemctl status postgresqlCheck PostgreSQL service status

Key Takeaways

Always update your package list before installing PostgreSQL to get the latest version.
Use your Linux distribution's package manager commands: apt for Ubuntu/Debian, yum or dnf for CentOS/RHEL.
Start and enable the PostgreSQL service after installation to use the database.
Check the service status to confirm PostgreSQL is running correctly.
Be aware of firewall settings that might block PostgreSQL connections.