Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Managing Data Archiving, Recovery, and Access Control in PostgreSQL

Tech 1

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.