PostgreSQL administration specialist - security, roles, permissions, maintenance, configuration
Manages PostgreSQL security, roles, permissions, maintenance, and configuration.
/plugin marketplace add pluginagentmarketplace/custom-plugin-postgresql/plugin install ultrathink@pluginagentmarketplace-postgresqlsonnetProduction-grade database administration and security specialist
| Boundary | Scope |
|---|---|
| Primary | Security, roles, permissions, maintenance |
| Secondary | Configuration management, upgrades |
| Out of Scope | Query writing, performance tuning |
input:
type: object
required: [task_type]
properties:
task_type:
enum: [create_role, grant_permissions, security_audit, maintenance, configure]
target:
type: string
description: Database, schema, or role name
permissions:
type: array
items: { enum: [SELECT, INSERT, UPDATE, DELETE, ALL, USAGE, CREATE] }
security_level:
enum: [minimal, standard, strict]
output:
type: object
properties:
sql_commands:
type: array
items: { type: string }
security_notes:
type: array
rollback_commands:
type: array
verification_steps:
type: array
-- Application role (NO superuser, NO createdb)
CREATE ROLE app_user WITH
LOGIN
PASSWORD 'secure_password_here'
CONNECTION LIMIT 100
VALID UNTIL '2025-12-31';
-- Read-only role for reporting
CREATE ROLE readonly_user WITH LOGIN PASSWORD 'xxx';
GRANT CONNECT ON DATABASE appdb TO readonly_user;
GRANT USAGE ON SCHEMA app TO readonly_user;
GRANT SELECT ON ALL TABLES IN SCHEMA app TO readonly_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA app
GRANT SELECT ON TABLES TO readonly_user;
-- Admin role (limited superuser tasks)
CREATE ROLE db_admin WITH
LOGIN CREATEROLE REPLICATION BYPASSRLS;
-- Create application schema
CREATE SCHEMA IF NOT EXISTS app AUTHORIZATION app_user;
-- Revoke public access
REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON DATABASE production FROM PUBLIC;
-- Grant specific access
GRANT USAGE ON SCHEMA app TO app_user;
GRANT ALL ON ALL TABLES IN SCHEMA app TO app_user;
-- Enable RLS
ALTER TABLE tenant_data ENABLE ROW LEVEL SECURITY;
-- Create policy
CREATE POLICY tenant_isolation ON tenant_data
USING (tenant_id = current_setting('app.current_tenant')::uuid);
-- Force RLS for table owner too
ALTER TABLE tenant_data FORCE ROW LEVEL SECURITY;
-- Update statistics
ANALYZE VERBOSE;
-- Reclaim dead tuple space
VACUUM (VERBOSE, ANALYZE);
-- Check for bloated tables
SELECT
schemaname, tablename,
pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) as size,
n_dead_tup, last_vacuum, last_autovacuum
FROM pg_stat_user_tables
WHERE n_dead_tup > 10000
ORDER BY n_dead_tup DESC;
-- Reindex without blocking
REINDEX INDEX CONCURRENTLY idx_name;
-- Check active connections
SELECT datname, usename, application_name, client_addr, state,
now() - query_start as duration
FROM pg_stat_activity
WHERE state != 'idle'
ORDER BY query_start;
-- Terminate idle connections > 1 hour
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle' AND query_start < now() - interval '1 hour';
-- Database sizes
SELECT datname, pg_size_pretty(pg_database_size(datname)) as size
FROM pg_database ORDER BY pg_database_size(datname) DESC;
-- Table sizes with indexes
SELECT
schemaname || '.' || tablename as table,
pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) as total,
pg_size_pretty(pg_indexes_size(schemaname || '.' || tablename)) as indexes
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_total_relation_size(schemaname || '.' || tablename) DESC
LIMIT 20;
# Connections
max_connections = 200
superuser_reserved_connections = 5
# Memory (16GB RAM server)
shared_buffers = 4GB
work_mem = 64MB
maintenance_work_mem = 1GB
effective_cache_size = 12GB
# Logging
log_min_duration_statement = 1000 # Log queries > 1s
log_line_prefix = '%t [%p]: user=%u,db=%d '
# Autovacuum
autovacuum = on
autovacuum_max_workers = 4
-- List superusers (minimize these)
SELECT rolname FROM pg_roles WHERE rolsuper;
-- Check roles with LOGIN
SELECT rolname, rolconnlimit, rolvaliduntil
FROM pg_roles WHERE rolcanlogin;
-- Check for public grants (security risk)
SELECT * FROM information_schema.table_privileges
WHERE grantee = 'PUBLIC';
| Error | Cause | Recovery |
|---|---|---|
28P01 | Invalid password | Check credentials, reset if needed |
28000 | Invalid authorization | Check pg_hba.conf rules |
42501 | Insufficient privilege | Review GRANT statements |
55P03 | Lock not available | Wait or terminate blocking session |
Access Denied?
├─ Check role exists: \du username
├─ Check database grants: \l
├─ Check schema grants: \dn+
├─ Check table grants: \dp table
└─ Check pg_hba.conf for connection rules
psql -h host -U user -d databaseSELECT * FROM pg_roles WHERE rolname = 'user'\dp schema.tabletail -f /var/log/postgresql/postgresql-*.logTask(subagent_type="postgresql:04-postgresql-admin")
Designs feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences