Install PostgreSQL Linux
To learn this step-by-step, check out our YouTube video and see how to install and set up PostgreSQL on Linux easily.
Installation Steps and Key Commands
# =========================================================
# PostgreSQL 16 FULL INSTALLATION AND BASIC USAGE ON RHEL 10
# =========================================================
# -------------------------------
# Check kernel and system details
# -------------------------------
uname -a
# -------------------------------
# Step 1: Add PostgreSQL 16 Repository
# -------------------------------
# Install PostgreSQL official Yum repository for RHEL 10
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-10-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Disable the default PostgreSQL module to prevent conflicts
sudo dnf -qy module disable postgresql
# -------------------------------
# Step 2: Install PostgreSQL 16
# -------------------------------
# Install PostgreSQL 16 server and client
sudo yum install -y postgresql16-server postgresql16
# -------------------------------
# Step 3: Initialize and Start PostgreSQL Server
# -------------------------------
# Initialize the PostgreSQL 16 database cluster
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
# Enable PostgreSQL to start automatically on boot
sudo systemctl enable postgresql-16
# Start PostgreSQL service
sudo systemctl start postgresql-16
# Check PostgreSQL service status
sudo systemctl status postgresql-16
# -------------------------------
# Step 4: Switch to the postgres user for database operations
# -------------------------------
sudo -i -u postgres
# -------------------------------
# Step 5: Create a test database and table for validation
# -------------------------------
psql -c "CREATE DATABASE sampledb;"
psql -d sampledb -c "CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT NOT NULL, department TEXT NOT NULL);"
psql -d sampledb -c "INSERT INTO employees (name, department) VALUES ('Alice', 'Engineering'), ('Bob', 'Marketing'), ('Charlie', 'Finance');"
psql -d sampledb -c "SELECT * FROM employees;"
# -------------------------------
# Step 6: Load the dvdrental Sample Database
# -------------------------------
# Download the dvdrental sample database archive
curl -O https://neon.com/postgresqltutorial/dvdrental.zip
# Install unzip if not installed
sudo yum install -y unzip
# Extract the downloaded dvdrental.zip
unzip dvdrental.zip
# Extract if a .tar.gz file was extracted
if [ -f dvdrental.tar.gz ]; then
tar -xvzf dvdrental.tar.gz
fi
# Extract if a .tar file was extracted
if [ -f dvdrental.tar ]; then
tar -xvf dvdrental.tar
fi
# Create the dvdrental database
psql -c "CREATE DATABASE dvdrental;"
# Restore the dvdrental database
pg_restore -U postgres --dbname=dvdrental --verbose dvdrental.tar
# -------------------------------
# Step 7: Basic PostgreSQL Exploration Commands
# -------------------------------
# List all available databases
psql -l
# Connect and check a sample query from the dvdrental database
psql -d dvdrental -c "SELECT COUNT(*) FROM film;"
# -------------------------------
# Additional Basic PostgreSQL Commands for Practice
# -------------------------------
# Connect to PostgreSQL interactive shell
psql
# Inside psql:
# List all databases
\l
# Connect to a database
\c dvdrental
# List all tables in the current database
\dt
# List all users/roles
\du
# Check current user
SELECT current_user;
# Describe a table structure
\d film
# Exit psql
\q
# Exit the postgres user shell
exit
# =========================================================
# PostgreSQL 16 is now fully installed and verified with
# the dvdrental sample database loaded and ready for practice.
# =========================================================