Managing Data Archiving, Recovery, and Access Control in PostgreSQL
Accumulated historical data degrades database performance and consumes significant storage. Archiving relocates infrequently accessed records to external mediums, maintaining optimal query speeds for active datasets while preserving data for compliance or analytical requirements.
Data Archiving Techniques
PostgreSQL offers multiple mechanisms for extracting and archiving data. A standard approach uses the COPY command to export specific record sets to external files.
COPY (SELECT * FROM transaction_logs WHERE created_at < CURRENT_DATE - INTERVAL '90 days')
TO '/var/lib/postgresql/archives/transactions_older_90d.csv' WITH CSV HEADER;
For comprehensive schema and data extraction, the pg_dump utility generates custom-format archive files.
pg_dump -h db_primary -p 5432 -U admin_user -F c -b -v -f /mnt/backups/db_archive.dump analytics_db
Data Restoration Procedures
Restorasion requires identifying the target scope—whether a single table or a complete database—and loading the appropriate backup artifacts.
To ingest data from a previously generated CSV archive:
COPY transaction_logs FROM '/var/lib/postgresql/restores/transactions_older_90d.csv' WITH CSV HEADER;
Custom-format dumps generated by pg_dump are restored using pg_restore:
pg_restore -h db_secondary -p 5432 -U admin_user -d analytics_db /mnt/backups/db_archive.dump
Access Control for Archiving and Recovery
Executing archiving and recovery operations necessitates strict privilege management.
For the COPY command, reading from or writing to local server files requires superuser privileges or the predefined roles pg_read_server_files and pg_write_server_files introduced in PostgreSQL 11+.
GRANT pg_write_server_files TO archive_operator;
GRANT pg_read_server_files TO restore_operator;
When using pg_dump, the connecting user must possess SELECT privileges on all targeted tables and schemas. To restore via pg_restore, the user needs CREATE privileges on the destination database and INSERT/UPDATE capabilities on the affected tables. To limit exposure, avoid running these utilities as a superuser; instead, create dedicated roles with the minimum required permissions.