🐘
Database & Messaging

PostgreSQL

Advanced open-source relational database

🐧 Linux🍎 Mac🪟 Windows📋 Quick reference →
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

What is this?

PostgreSQL is a powerful open-source database used by apps that need reliable, complex data storage.

📁

Config files for PostgreSQL

Where to create or edit the main configuration — paths below match the setup steps.

  • /etc/postgresql/*/main/postgresql.conf

    Location: PostgreSQL server

    Memory, connections, logging

  • /etc/postgresql/*/main/pg_hba.conf

    Location: PostgreSQL server

    Client authentication rules

📥

Step 01

Install PostgreSQL

(01)Install

Linux
1sudo apt-get install -y postgresql postgresql-contrib
2sudo systemctl enable postgresql
⚙️

Step 02

Configure PostgreSQL

(01)Create database and user

Linux
1sudo -u postgres psql
2CREATE USER myuser WITH PASSWORD 'mypass';
3CREATE DATABASE mydb OWNER myuser;
4\q

Step 03

Verify

(01)Connect

Linux
1psql -U myuser -d mydb -c 'SELECT version();'

Step 04

Manage

(01)Backup

Linux
1pg_dump mydb > backup.sql
2psql mydb < backup.sql

📋Config templates

4 YAML templates for PostgreSQL. Copy and deploy after setup.

4 ready-to-copy templates. Expand one, copy the YAML, then run the deploy commands.

docker-compose.yaml

PostgreSQL database + Flask app with env file and volume persistence

yaml
1version: "3.9"
2
3services:
4 db:
5 image: postgres:15-alpine
6 environment:
7 POSTGRES_DB: dukaan_db
8 POSTGRES_USER: dukaan
9 POSTGRES_PASSWORD: dukaan123
10 ports:
11 - "5432:5432"
12 volumes:
13 - pgdata:/var/lib/postgresql/data
14
15 app:
16 build: .
17 ports:
18 - "5000:5000"
19 env_file:
20 - .env
21 depends_on:
22 - db
23
24volumes:
25 pgdata:

Dockerfile

Flask application container image

dockerfile
1FROM python:3.12-slim
2WORKDIR /app
3COPY requirements.txt .
4RUN pip install --no-cache-dir -r requirements.txt
5COPY . .
6EXPOSE 5000
7CMD ["python", "app.py"]
📄

Step 01

Run with Docker Compose

(01)Build and start services

Linux
1cd your-project/
2docker compose up -d --build
3docker compose ps
4docker compose logs -f app

(02)Stop and clean up

Linux
1docker compose down
2docker compose down -v # remove volumes