| name | postgres |
| description | PostgreSQL CLI client (psql) for database operations running inside a Docker container |
| runtime | docker |
PostgreSQL Skill
Access PostgreSQL databases using the psql CLI client running inside a Docker container. Execute queries, manage databases, run scripts, export/import data, and perform database operations without installing PostgreSQL locally.
Overview
This skill provides the psql command-line client through a containerized runtime. Connect to any PostgreSQL database (local or remote), execute SQL commands, run migration scripts, and manage your databases - all while keeping your host system clean.
When to Use This Skill
Use this skill when you need to:
- Connect to PostgreSQL databases without local installation
- Execute SQL queries and commands
- Run database migration scripts
- Export and import data (CSV, SQL dumps)
- List databases, tables, and schemas
- Inspect table structures and relationships
- Execute administrative tasks
- Test database connections
- Run ad-hoc queries during development
Prerequisites
Docker
The PostgreSQL client runs inside a Docker container:
brew install --cask docker
sudo apt-get update
sudo apt-get install docker.io
sudo systemctl start docker
PostgreSQL Server
You need access to a PostgreSQL server:
- Local PostgreSQL instance
- Remote database server
- Cloud-hosted PostgreSQL (AWS RDS, Google Cloud SQL, Azure, etc.)
- Docker PostgreSQL container
Configuration
Add to your .skill-engine.toml:
[skills.postgres]
source = "docker:postgres:16-alpine"
runtime = "docker"
description = "PostgreSQL CLI client"
[skills.postgres.docker]
image = "postgres:16-alpine"
entrypoint = "psql"
environment = ["PGPASSWORD=${PGPASSWORD:-}"]
network = "bridge"
memory = "256m"
rm = true
Configuration Explained
- image:
postgres:16-alpine - PostgreSQL 16.x Alpine image (~80MB)
- entrypoint:
psql - PostgreSQL interactive terminal
- environment: Sets
PGPASSWORD from host environment
- network:
bridge - Allows connections to databases (local and remote)
- memory:
256m - Limits container to 256MB RAM
- rm:
true - Automatically removes container after execution
Usage
PostgreSQL skill uses pass-through syntax - all arguments after -- are passed directly to psql:
skill run postgres -- [psql arguments]
Connection Methods
Using Command-Line Arguments
skill run postgres -- -h localhost -U postgres -d mydb
skill run postgres -- -h localhost -p 5432 -U myuser -d mydb
skill run postgres -- -h db.example.com -U admin -d production
Using Environment Variables
export PGHOST=localhost
export PGPORT=5432
export PGUSER=postgres
export PGDATABASE=mydb
export PGPASSWORD=secret
skill run postgres --
Using Connection URI
skill run postgres -- postgresql://user:password@host:5432/database
skill run postgres -- "postgresql://user:password@host:5432/database?sslmode=require"
Common Operations
Running Queries
skill run postgres -- -h localhost -U postgres -c "SELECT * FROM users;"
skill run postgres -- -h localhost -U postgres -c "SELECT count(*) FROM users; SELECT version();"
skill run postgres -- -h localhost -U postgres -c "SELECT * FROM users;" -x
Database Management
skill run postgres -- -h localhost -U postgres -l
skill run postgres -- -h localhost -U postgres -c "CREATE DATABASE newdb;"
skill run postgres -- -h localhost -U postgres -c "DROP DATABASE olddb;"
skill run postgres -- -h localhost -U postgres -d mydb
Table Operations
skill run postgres -- -h localhost -U postgres -d mydb -c "\dt"
skill run postgres -- -h localhost -U postgres -d mydb -c "\dt+"
skill run postgres -- -h localhost -U postgres -d mydb -c "\d users"
skill run postgres -- -h localhost -U postgres -d mydb -c "\d+ users"
Schema Operations
skill run postgres -- -h localhost -U postgres -d mydb -c "\dn"
skill run postgres -- -h localhost -U postgres -d mydb -c "\dt myschema.*"
skill run postgres -- -h localhost -U postgres -d mydb -c "SET search_path TO myschema,public;"
Executing SQL Files
skill run postgres -- -h localhost -U postgres -d mydb -f migration.sql
skill run postgres -- -h localhost -U postgres -d mydb -f script.sql -v ON_ERROR_STOP=1
for file in migrations/*.sql; do
skill run postgres -- -h localhost -U postgres -d mydb -f "$file"
done
Data Export
skill run postgres -- -h localhost -U postgres -d mydb -c "COPY users TO STDOUT CSV HEADER" > users.csv
skill run postgres -- -h localhost -U postgres -d mydb -c "COPY (SELECT * FROM users WHERE active=true) TO STDOUT CSV HEADER" > active_users.csv
skill run postgres -- -h localhost -U postgres -d mydb -c "COPY users TO STDOUT CSV HEADER DELIMITER '|'" > users.csv
skill run postgres -- -h localhost -U postgres -d mydb -c "\copy users TO 'users.sql'"
Data Import
skill run postgres -- -h localhost -U postgres -d mydb -c "COPY users FROM STDIN CSV HEADER" < users.csv
skill run postgres -- -h localhost -U postgres -d mydb -c "COPY users(name,email) FROM STDIN CSV HEADER" < partial_users.csv
skill run postgres -- -h localhost -U postgres -d mydb -f dump.sql
User Management
skill run postgres -- -h localhost -U postgres -c "\du"
skill run postgres -- -h localhost -U postgres -c "CREATE USER newuser WITH PASSWORD 'password';"
skill run postgres -- -h localhost -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE mydb TO newuser;"
skill run postgres -- -h localhost -U postgres -c "ALTER USER myuser WITH PASSWORD 'newpassword';"
Indexes & Performance
skill run postgres -- -h localhost -U postgres -d mydb -c "\di"
skill run postgres -- -h localhost -U postgres -d mydb -c "CREATE INDEX idx_email ON users(email);"
skill run postgres -- -h localhost -U postgres -d mydb -c "EXPLAIN ANALYZE SELECT * FROM users WHERE email='test@example.com';"
skill run postgres -- -h localhost -U postgres -d mydb -c "VACUUM ANALYZE;"
Meta-Commands
PostgreSQL psql supports special backslash commands:
Information Commands
skill run postgres -- -h localhost -U postgres -c "\l"
skill run postgres -- -h localhost -U postgres -d mydb -c "\dt"
skill run postgres -- -h localhost -U postgres -d mydb -c "\dv"
skill run postgres -- -h localhost -U postgres -d mydb -c "\df"
skill run postgres -- -h localhost -U postgres -d mydb -c "\dn"
skill run postgres -- -h localhost -U postgres -c "\du"
skill run postgres -- -h localhost -U postgres -d mydb -c "\d users"
Output Formatting
skill run postgres -- -h localhost -U postgres -d mydb -c "\x" -c "SELECT * FROM users;"
skill run postgres -- -h localhost -U postgres -d mydb -c "\t" -c "SELECT name FROM users;"
skill run postgres -- -h localhost -U postgres -d mydb -c "\a" -c "SELECT * FROM users;"
Advanced Examples
Database Backup
docker run --rm postgres:16-alpine pg_dump -h host -U user -d mydb > backup.sql
docker run --rm postgres:16-alpine pg_dump -h host -U user -d mydb -t users > users_backup.sql
docker run --rm postgres:16-alpine pg_dump -h host -U user -d mydb --schema-only > schema.sql
docker run --rm postgres:16-alpine pg_dump -h host -U user -d mydb --data-only > data.sql
Database Restore
skill run postgres -- -h localhost -U postgres -d mydb -f backup.sql
docker run --rm -i postgres:16-alpine pg_restore -h host -U user -d mydb -v < backup.dump
Batch Operations
skill run postgres -- -h localhost -U postgres -d mydb << EOF
BEGIN;
UPDATE users SET status='active' WHERE last_login > NOW() - INTERVAL '30 days';
DELETE FROM sessions WHERE expires_at < NOW();
COMMIT;
EOF
Connection Testing
skill run postgres -- -h localhost -U postgres -c "SELECT version();"
skill run postgres -- -h localhost -U postgres -c "SELECT current_database(), current_user, inet_server_addr(), inet_server_port();"
skill run postgres -- -h localhost -U postgres -c "SELECT pg_is_in_recovery();"
Query Monitoring
skill run postgres -- -h localhost -U postgres -c "SELECT pid, usename, query, state FROM pg_stat_activity WHERE state='active';"
skill run postgres -- -h localhost -U postgres -c "SELECT datname, pg_size_pretty(pg_database_size(datname)) FROM pg_database;"
skill run postgres -- -h localhost -U postgres -d mydb -c "SELECT schemaname, tablename, pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size FROM pg_tables ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC LIMIT 10;"
Environment Variables
PostgreSQL Standard Variables
| Variable | Description | Example |
|---|
PGHOST | Database host | localhost |
PGPORT | Database port | 5432 |
PGUSER | Username | postgres |
PGPASSWORD | Password | secret |
PGDATABASE | Database name | mydb |
PGSSLMODE | SSL mode | require, prefer, disable |
Usage
export PGHOST=localhost
export PGUSER=postgres
export PGPASSWORD=secret
export PGDATABASE=mydb
skill run postgres --
PGHOST=localhost PGPASSWORD=secret skill run postgres -- -U postgres -c "SELECT 1;"
Security Best Practices
Password Management
export PGPASSWORD=your_password
skill run postgres -- -h host -U user -d db
chmod 600 ~/.pgpass
skill run postgres -- "postgresql://user:pass@host:5432/db"
SSL Connections
skill run postgres -- "postgresql://user:pass@host:5432/db?sslmode=require"
skill run postgres -- "postgresql://user:pass@host:5432/db?sslmode=verify-ca"
skill run postgres -- "postgresql://user:pass@host:5432/db?sslmode=verify-full"
Read-Only Access
skill run postgres -- -h host -U user -d db -c "BEGIN READ ONLY; SELECT * FROM sensitive_table; COMMIT;"
skill run postgres -- -h host -U postgres -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;"
Troubleshooting
Connection Refused
docker ps | grep postgres
ping db-host
telnet localhost 5432
Authentication Failed
skill run postgres -- -h localhost -U postgres -c "SELECT 1;"
echo $PGPASSWORD
skill run postgres -- -h localhost -U postgres -W
Permission Denied
skill run postgres -- -h localhost -U postgres -c "\du"
skill run postgres -- -h localhost -U postgres -c "GRANT ALL ON DATABASE mydb TO myuser;"
Cannot Connect to Docker Network
skill run postgres -- -h host.docker.internal -U postgres
Common Errors
| Error | Cause | Solution |
|---|
| "connection refused" | PostgreSQL not running or wrong port | Check server status and port |
| "password authentication failed" | Wrong credentials | Verify username/password |
| "database does not exist" | Database not created | Use -l to list databases |
| "permission denied" | Insufficient privileges | Check user grants |
| "could not connect to server" | Network issue or wrong host | Verify host and network connectivity |
PostgreSQL Client Tools
The container includes additional tools:
pg_dump (Backup)
docker run --rm -e PGPASSWORD=secret postgres:16-alpine pg_dump -h localhost -U postgres mydb > backup.sql
pg_restore (Restore)
docker run --rm -e PGPASSWORD=secret -i postgres:16-alpine pg_restore -h localhost -U postgres -d mydb < backup.dump
createdb / dropdb
docker run --rm -e PGPASSWORD=secret postgres:16-alpine createdb -h localhost -U postgres newdb
docker run --rm -e PGPASSWORD=secret postgres:16-alpine dropdb -h localhost -U postgres olddb
Docker Image Details
- Image:
postgres:16-alpine
- Base: Alpine Linux
- Size: ~80MB
- PostgreSQL Version: 16.x
- Included Tools: psql, pg_dump, pg_restore, createdb, dropdb, pg_isready
- Platforms: linux/amd64, linux/arm64
Resources
Quick Reference
Essential Commands Table
| Operation | Command |
|---|
| Connect | -h host -U user -d database |
| Run query | -c "SELECT * FROM table" |
| Run script | -f script.sql |
| List databases | -l |
| List tables | -c "\dt" |
| Describe table | -c "\d tablename" |
| Export CSV | -c "COPY table TO STDOUT CSV HEADER" > file.csv |
| Import CSV | -c "COPY table FROM STDIN CSV HEADER" < file.csv |
| Backup database | pg_dump -h host -U user db > backup.sql |
| Restore database | -f backup.sql |