Postgres
π PostgreSQL Cheat Sheet
[!info] 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 psql
Set Password
\password
-- or
ALTER USER postgres WITH PASSWORD 'SuperSecret';
π Enable Password Auth (pg_hba.conf)
Edit:
sudo vi /etc/postgresql/12/main/pg_hba.conf
Change:
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.gz
Custom format:
pg_dump -h host -U user -d dbname -F c -f /tmp/db.dmp
Restore SQL:
psql -d newdb -f db.sql
π¦ pg_dumpall
Full instance backup:
pg_dumpall -h host -U postgres > database.out
Restore:
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 -v
Related
Explore More π
Tags π
#postgresql #database #sql #kubernetes