π PostgreSQL Cheat Sheet
PostgreSQL is a powerful open-source relational database known for reliability, feature robustness, and performance. This sheet covers essential commands and configuration tips to get started and work efficiently.
βοΈ Overview
PostgreSQL, or Postgres, supports advanced data types, powerful functions, and ACID compliance. Itβs ideal for transactional applications, BI tools, and scalable web services.
π Features
-
ACID-compliant transactions
-
JSONB and XML support
-
Materialized views and triggers
-
Full-text search
-
Custom functions and stored procedures
-
Extensions like PostGIS, TimescaleDB
π§° Getting Started
π οΈ Installation (Ubuntu 20.04)
sudo apt update
sudo apt install -y postgresql postgresql-contrib postgresql-client
sudo systemctl status postgresql.serviceβοΈ Install on Kubernetes via Zalando Operator
Example
Useful for production-grade PostgreSQL in Kubernetes environments.
Resources:
π Initial Access & Configuration
Connect to PostgreSQL
sudo -u postgres psqlSet Password
\password
-- or
ALTER USER postgres WITH PASSWORD 'SuperSecret';π Enable Password Auth (pg_hba.conf)
Edit:
sudo vi /etc/postgresql/12/main/pg_hba.confChange:
local all postgres peer
to:
local all postgres md5
Restart:
sudo systemctl restart postgresqlπ₯ User & Database Management
Create User
CREATE USER myuser WITH ENCRYPTED PASSWORD 'SuperSecret';Create Database
CREATE DATABASE dbname OWNER myuser;Change Owner of Existing DB
ALTER DATABASE dbname OWNER TO myuser;πΎ Backup & Restore
π§³ pg_dump
SQL file:
pg_dump -h host -U user -d dbname --create -Z 9 -f /tmp/db.sql.gzCustom format:
pg_dump -h host -U user -d dbname -F c -f /tmp/db.dmpRestore SQL:
psql -d newdb -f db.sqlπ¦ pg_dumpall
Full instance backup:
pg_dumpall -h host -U postgres > database.outRestore:
psql -h host -U postgres -f database.outπ οΈ pg_restore
Restore from custom format:
pg_restore -h host -U user -d postgres --create -F c /tmp/db.dmp -vRelated
Explore More π
Tags π
postgresql database sql kubernetes