postgresql
PostgreSQL administration and setup including installation, configuration, backup/recovery, replication, and high-availability. Learn production PostgreSQL operations.
What this skill does
# PostgreSQL Administration
## Installation & Setup
```bash
# On Linux (Ubuntu/Debian)
sudo apt-get install postgresql postgresql-contrib
# On macOS
brew install postgresql@15
# Docker installation
docker run --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:15
# Start and enable PostgreSQL
sudo systemctl start postgresql
sudo systemctl enable postgresql
```
## Connection Basics
```bash
# Connect to default database
psql -U postgres
# Connect to specific database
psql -U postgres -d mydb -h localhost -p 5432
# List databases
\l
# List tables in current database
\dt
# Get table info
\d table_name
# Quit psql
\q
```
## User & Role Management
```sql
-- Create a new role
CREATE ROLE developer WITH LOGIN PASSWORD 'secure_password';
-- Create superuser role
CREATE ROLE admin WITH SUPERUSER LOGIN PASSWORD 'admin_password';
-- Grant privileges on database
GRANT CONNECT ON DATABASE mydb TO developer;
-- Grant privileges on schema
GRANT USAGE ON SCHEMA public TO developer;
-- Grant privileges on tables
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public TO developer;
-- Grant privileges on specific table
GRANT SELECT ON employees TO developer;
-- Make role a database owner
ALTER DATABASE mydb OWNER TO developer;
-- Revoke privileges
REVOKE INSERT, UPDATE ON employees FROM developer;
-- Drop role
DROP ROLE developer;
```
## Configuration & Tuning
```bash
# PostgreSQL configuration file
sudo nano /etc/postgresql/15/main/postgresql.conf
# Key configuration parameters:
# Memory
shared_buffers = 256MB # 25% of RAM for dedicated server
effective_cache_size = 1GB # 50-75% of RAM
work_mem = 64MB # RAM per operation
# Connections
max_connections = 200
superuser_reserved_connections = 3
# Write-Ahead Log
wal_level = replica
max_wal_senders = 3
wal_keep_segments = 64
# Query planning
random_page_cost = 1.1 # For SSD
log_min_duration_statement = 1000 # Log slow queries
```
## Backup & Recovery
```bash
# Full database backup (text format)
pg_dump -U postgres -d mydb -f mydb_backup.sql
# Binary backup (faster, compressed)
pg_dump -U postgres -d mydb -Fc -f mydb_backup.dump
# Backup specific table
pg_dump -U postgres -d mydb -t employees -f employees_backup.sql
# Backup all databases
pg_dumpall -U postgres -f all_databases.sql
# Restore from backup
psql -U postgres -d mydb -f mydb_backup.sql
# Restore from binary dump
pg_restore -U postgres -d mydb mydb_backup.dump
```
## Maintenance Operations
```sql
-- VACUUM (reclaim space)
VACUUM; -- Full vacuum
-- VACUUM ANALYZE (reclaim space & update stats)
VACUUM ANALYZE;
-- ANALYZE (update table statistics)
ANALYZE;
-- Check database integrity
REINDEX DATABASE mydb;
-- Show database size
SELECT pg_database.datname,
pg_size_pretty(pg_database_size(pg_database.datname))
FROM pg_database;
-- Show table sizes
SELECT schemaname, tablename,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename))
FROM pg_tables
WHERE schemaname != 'pg_catalog'
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
```
## Monitoring
```sql
-- Active connections
SELECT * FROM pg_stat_activity WHERE state != 'idle';
-- Database statistics
SELECT * FROM pg_stat_database WHERE datname = 'mydb';
-- Table statistics
SELECT * FROM pg_stat_user_tables;
-- Index statistics
SELECT * FROM pg_stat_user_indexes;
-- Cache hit ratio
SELECT
sum(heap_blks_read) as heap_read,
sum(heap_blks_hit) as heap_hit,
sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) as ratio
FROM pg_statio_user_tables;
```
## Performance Tuning
```sql
-- Check slow queries
SELECT query, mean_exec_time, calls
FROM pg_stat_statements
ORDER BY mean_exec_time DESC LIMIT 10;
-- Find unused indexes
SELECT schemaname, tablename, indexname
FROM pg_stat_user_indexes
WHERE idx_scan = 0;
-- Find missing indexes
SELECT * FROM pg_stat_user_tables
WHERE seq_scan > idx_scan
AND n_live_tup > 1000;
-- Analyze query plan
EXPLAIN ANALYZE
SELECT * FROM employees WHERE salary > 50000;
```
## Replication Setup
```bash
# On primary server - enable replication in postgresql.conf
wal_level = replica
max_wal_senders = 3
wal_keep_segments = 64
# Create replication user
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'rep_password';
# On standby - base backup from primary
pg_basebackup -h primary_host -D /var/lib/postgresql/15/main -U replicator -v -P -W
# Create recovery.conf on standby
standby_mode = 'on'
primary_conninfo = 'host=primary_host port=5432 user=replicator password=password'
```
## High Availability with pgBouncer
```bash
# Install pgBouncer
sudo apt-get install pgbouncer
# Configuration - /etc/pgbouncer/pgbouncer.ini
[databases]
mydb = host=primary_host port=5432 dbname=mydb
[pgbouncer]
listen_port = 6432
max_client_conn = 1000
default_pool_size = 25
reserve_pool_size = 5
```
## Next Steps
Learn advanced security features including row-level security and SSL/TLS configuration in the `postgresql-security` skill.
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.