Back to all guides
Databases

How to Install and Secure PostgreSQL & Redis on Ubuntu 24.04

4 read2026-08-31

PostgreSQL is an advanced, enterprise-grade relational database, while Redis provides ultra-fast in-memory caching and message queuing. This guide covers installing and securing both on Ubuntu 22.04 / 24.04.

1. Install PostgreSQL

bash
sudo apt update
sudo apt install -y postgresql postgresql-contrib

Enable and verify PostgreSQL service:

bash
sudo systemctl enable --now postgresql
sudo systemctl status postgresql

2. Create PostgreSQL Database & User

Switch to the postgres user and enter the interactive SQL shell:

bash
sudo -u postgres psql

Run the following SQL commands:

sql
CREATE USER appuser WITH ENCRYPTED PASSWORD 'SuperSecretPassword123!';
CREATE DATABASE appdb OWNER appuser;
GRANT ALL PRIVILEGES ON DATABASE appdb TO appuser;
\q

3. Install & Configure Redis

Install Redis server:

bash
sudo apt install -y redis-server

Configure Redis memory limits and password authentication:

bash
sudo nano /etc/redis/redis.conf

Set or uncomment the following lines:

ini
supervised systemd
maxmemory 256mb
maxmemory-policy allkeys-lru
requirepass YourStrongRedisPasswordHere!

Restart Redis to apply changes:

bash
sudo systemctl restart redis-server

4. Test Connections

Test PostgreSQL:

bash
psql -U appuser -d appdb -h 127.0.0.1 -W

Test Redis:

bash
redis-cli -a "YourStrongRedisPasswordHere!" ping
# Returns: PONG
KVM Linux VPS

Looking for the right Linux VPS?

Put your project into practice right away: Our KVM VPS provide full root access, lightning-fast NVMe storage, and reliable KVM isolation. Flexible prepaid billing with no subscription and personal Discord support.

Full Root Access (SSH)
Datacenter NVMe SSD
4+ Tbps DDoS Protection
100% Prepaid (No Subscription)