dcg

PostgreSQL

database.postgresql

Protects against destructive PostgreSQL operations like DROP DATABASE, TRUNCATE, and dropdb

  • 10blocking rules
  • 2safe patterns
  • 3Critical
  • 7High
~/.config/dcg/config.toml
[packs]
enabled = ["database.postgresql"]

Evaluated when a command contains psql, dropdb, DROP, TRUNCATE, pg_dump, postgres, DELETE, delete, drop, truncate.

Recorded verdicts

  • psql -c "DROP TABLE users"Blockeddatabase.postgresql:drop-table
  • dropdb productionBlockeddatabase.postgresql:dropdb-cli
  • psql -c "SELECT count(*) FROM users"Allowed

Blocking rules

Each rule ID is database.postgresql:<name>. Use it in allowlists and [policy.rules].

  1. drop-database

    Critical

    DROP DATABASE permanently deletes the entire database (even with IF EXISTS). Verify and back up first.

    Explanation
    DROP DATABASE completely removes a database and ALL its contents: - All tables, views, and indexes - All functions, procedures, and triggers - All data - gone permanently - Users/roles remain but lose access IF EXISTS only prevents errors if the database doesn't exist - it still deletes! Before dropping: pg_dump -h host -U user dbname > backup.sql Verify database name: psql -c '\l' | grep dbname
  2. drop-schema

    Critical

    DROP SCHEMA permanently deletes the schema and all its objects (even with IF EXISTS).

    Explanation
    DROP SCHEMA removes a schema and potentially ALL objects within it: - With CASCADE: Drops all tables, views, functions in the schema - With RESTRICT (default): Fails if schema is not empty - public schema deletion is catastrophic List schema contents first: SELECT table_name FROM information_schema.tables WHERE table_schema = 'schema_name'; Backup schema: pg_dump -n schema_name dbname > schema_backup.sql
  3. dropdb-cli

    Critical

    dropdb permanently deletes the entire database. Verify the database name carefully.

    Explanation
    dropdb is the CLI equivalent of DROP DATABASE: - Completely removes the database - All data is lost permanently - No confirmation prompt by default - Cannot be undone Triple-check the database name. Common mistake: dropdb myapp_production # Oops, meant myapp_staging Backup first: pg_dump -h host -U user dbname > backup.sql List databases to verify: psql -c '\l'
  4. delete-without-where

    High

    DELETE without WHERE clause deletes ALL rows. Add a WHERE clause or use TRUNCATE intentionally.

    Explanation
    DELETE without WHERE removes ALL rows from the table: - Each row deletion is logged (slower than TRUNCATE) - Can be rolled back within a transaction - Fires DELETE triggers for each row - Does not reset IDENTITY/SERIAL counters If you meant to delete all rows, use TRUNCATE for speed. Otherwise, add a WHERE clause: DELETE FROM tablename WHERE condition; Preview what would be deleted: SELECT COUNT(*) FROM tablename; -- all rows! SELECT * FROM tablename LIMIT 10;
  5. drop-column

    High

    ALTER TABLE ... DROP COLUMN permanently deletes that column's data in every row.

    Explanation
    Dropping a column removes its values from every row. PostgreSQL does not keep them anywhere you can restore from without a backup. Back up the column first: CREATE TABLE tablename_col_backup AS SELECT id, col FROM tablename; Or keep it for now: stop reading it in the application, then drop it later.
  6. drop-table

    High

    DROP TABLE permanently deletes the table (even with IF EXISTS). Verify and back up first.

    Explanation
    DROP TABLE removes the table structure and ALL data: - All rows are deleted - Indexes, constraints, triggers are removed - Foreign keys referencing this table may fail - CASCADE drops dependent objects too IF EXISTS only prevents errors - it still drops the table! Backup table first: pg_dump -t tablename dbname > table_backup.sql Preview table contents: SELECT COUNT(*) FROM tablename; SELECT * FROM tablename LIMIT 10;
  7. pg-dump-clean

    High

    pg_dump --clean drops objects before creating them. This can be destructive on restore.

    Explanation
    pg_dump --clean adds DROP statements to the backup file. On restore: - DROP TABLE is run before CREATE TABLE - Existing data is deleted before restore - If restore fails partway, data may be lost This is safe for backup, but dangerous when restoring to a database with existing data you want to keep. Safer approach for restoring: - Restore to a new database first - Verify the restore - Then swap databases Without --clean: pg_dump dbname > backup.sql # Creates only, no drops
  8. stdin-unverified

    High

    psql receives indirect input that dcg cannot statically verify.

    Explanation
    Materialize and review the exact SQL before piping or redirecting it into psql.
  9. truncate-table

    High

    TRUNCATE permanently deletes all rows without logging individual deletions.

    Explanation
    TRUNCATE is faster than DELETE but more dangerous: - Removes ALL rows instantly - Cannot be rolled back outside a transaction - Does not fire DELETE triggers - Resets IDENTITY/SERIAL columns - CASCADE truncates referencing tables too TRUNCATE is transactional in PostgreSQL. Wrap in transaction: BEGIN; TRUNCATE tablename; -- verify, then COMMIT or ROLLBACK Check row count first: SELECT COUNT(*) FROM tablename;
  10. update-without-where

    High

    UPDATE without WHERE clause overwrites the column in ALL rows. Add a WHERE clause.

    Explanation
    UPDATE without WHERE changes every row in the table, replacing whatever values were there. Unless it runs inside a transaction you then roll back, the previous values are gone. Scope it: UPDATE tablename SET col = value WHERE condition; Preview what would change: SELECT COUNT(*) FROM tablename WHERE condition; Or wrap it: BEGIN; UPDATE …; SELECT …; ROLLBACK/COMMIT;

Safe patterns

Checked first. A command that matches one is never blocked by this pack.

  • pg-dump-no-clean
  • select-query