PostgreSQL

PostgreSQL database configuration and details for Piglet Run.

Overview

Piglet Run includes PostgreSQL 17 as the primary database with optimized defaults.

Connection Details

ParameterDefault Value
Hostlocalhost
Port5432
Userdba
Databasepostgres
Socket/var/run/postgresql

Connection Strings

Local Connection

psql -U dba -d postgres

TCP Connection

postgresql://dba@localhost:5432/postgres

With Password

postgresql://dba:password@localhost:5432/postgres

Configuration

File: /data/postgres/postgresql.conf

Memory Settings

# Memory
shared_buffers = 256MB
effective_cache_size = 768MB
work_mem = 4MB
maintenance_work_mem = 64MB
huge_pages = try

Connection Settings

# Connections
listen_addresses = 'localhost'
port = 5432
max_connections = 100
superuser_reserved_connections = 3

WAL Settings

# WAL
wal_level = replica
max_wal_size = 1GB
min_wal_size = 80MB
wal_buffers = 8MB
checkpoint_completion_target = 0.9

Logging

# Logging
log_destination = 'csvlog'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_rotation_age = 1d
log_rotation_size = 100MB
log_statement = 'ddl'
log_min_duration_statement = 1000

Client Authentication

File: /data/postgres/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     trust
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256

Service Management

# Start PostgreSQL
pig start postgres

# Stop PostgreSQL
pig stop postgres

# Restart PostgreSQL
pig restart postgres

# Reload configuration
pig reload postgres

# View logs
pig logs postgres

Database Management

# Create database
pig db create mydb

# List databases
pig db list

# Drop database
pig db drop mydb

# Connect to database
pig db connect mydb

User Management

# Create user
pig user create myuser

# Grant privileges
psql -c "GRANT ALL ON DATABASE mydb TO myuser"

# Change password
pig user passwd myuser

Backup and Restore

# Full backup
pig backup db --full

# Point-in-time recovery
pig restore db --time "2024-01-15 14:30:00"

Performance Tuning

RAMshared_bufferseffective_cache_sizework_mem
4GB1GB3GB32MB
8GB2GB6GB64MB
16GB4GB12GB128MB
32GB8GB24GB256MB

Monitoring

# Connection statistics
psql -c "SELECT * FROM pg_stat_activity"

# Database size
psql -c "SELECT pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) FROM pg_database"

# Table statistics
psql -c "SELECT * FROM pg_stat_user_tables"

See Also