Introduction
PostgreSQL (often referred to as Postgres) is an advanced, open-source relational database management system (RDBMS) known for its stability, extensibility, and standards compliance. It is widely used in both small applications and large-scale enterprise systems.
This guide walks through installing and configuring PostgreSQL on a Linux server, focusing on:
- Provisioning a Linux server on Microsoft Azure
- Installing PostgreSQL from official repositories
- Creating roles and databases
- Securing the installation
- Setting up remote access (optional)
Prerequisites
- A running Linux server (Ubuntu 20.04+, Debian 10+, CentOS/RHEL 8+)
- A user with
sudo
privileges - Internet connectivity to install packages
Step 0: Provisioning a Linux Virtual Machine on Microsoft Azure
0.1 Sign in to Azure Portal
Go to https://portal.azure.com.
Create an account if you don't have one. A free-tier VM is available.
0.2 Create a Linux Virtual Machine
- Go to Virtual Machines > Create > Azure Virtual Machine
- Fill in the Basics:
- Subscription: Select your Azure subscription
- Resource Group: Create or choose one
-
VM Name:
pg-server
- Region: Nearest to your users
- Image: Ubuntu 22.04 LTS
-
Size:
B1s (1 vCPU, 1 GB RAM)
for testing - Authentication: Use SSH public key (recommended)
-
Username:
azureuser
- On Disks, select Standard SSD
- On Networking, ensure SSH (port 22) is open
- Click Review + Create, then Create
0.3 Connect to Your VM
Once deployed:
- Copy the public IP address of the VM
- Open a terminal or SSH tool and connect:
ssh azureuser@<VM_IP_ADDRESS>
Installing PostgreSQL
For Ubuntu/Debian-based Distributions:
Step 1: Update system packages
sudo apt update && sudo apt upgrade -y
Step 2: Install PostgreSQL
sudo apt install postgresql postgresql-contrib -y
This installs both the PostgreSQL server and extra utilities like pgadmin
, pg_basebackup
, etc.
For CentOS/RHEL-based Distributions:
Step 1: Enable the PostgreSQL repository
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Step 2: Disable built-in PostgreSQL
sudo dnf -qy module disable postgresql
Step 3: Install PostgreSQL (e.g., version 15)
sudo dnf install -y postgresql15-server postgresql15
Step 4: Initialize and start PostgreSQL
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable --now postgresql-15
Creating Roles and Databases
PostgreSQL uses a role-based authentication system.
Switch to the postgres user:
sudo -i -u postgres
Create a new database user:
createuser --interactive
Create a database:
createdb my_database
Connect to the PostgreSQL prompt:
psql
Inside the psql
shell, you can run:
\l -- List all databases
\du -- List all roles
\q -- Quit
Securing PostgreSQL
Change password for postgres
:
psql -c "ALTER USER postgres PASSWORD 'StrongPasswordHere';"
Configure pg_hba.conf
(host-based access):
Ubuntu:
sudo nano /etc/postgresql/*/main/pg_hba.conf
CentOS/RHEL:
sudo nano /var/lib/pgsql/15/data/pg_hba.conf
Change:
local all postgres peer
To:
local all postgres md5
Restart PostgreSQL:
sudo systemctl restart postgresql
Allowing Remote Connections (Optional)
Step 1: Modify postgresql.conf
Ubuntu:
sudo nano /etc/postgresql/*/main/postgresql.conf
CentOS/RHEL:
sudo nano /var/lib/pgsql/15/data/postgresql.conf
Set:
listen_addresses = '*'
Step 2: Update pg_hba.conf
Add:
host all all 0.0.0.0/0 md5
Be cautious with
0.0.0.0/0
. Restrict this to your IP range in production environments.
Step 3: Restart PostgreSQL and open firewall
sudo ufw allow 5432/tcp
sudo systemctl restart postgresql
Testing the Installation
Local access:
psql -U postgres
Remote access:
psql -h <your_server_ip> -U <your_user> -d <your_database>
Conclusion
PostgreSQL is a robust, feature-rich database that integrates well with modern applications. By following these steps, you have:
- Provisioned a Linux server on Azure
- Installed PostgreSQL
- Set up secure users and databases
- Configured the database for local and remote access
Top comments (0)