Restore a database

Restore in 4 parallel jobs and drop existing objects first

pg_restore -U myuser -d mydb -j 4 --clean mydb.dump

tool
postgres29 commands
tags
postgres

More postgres commands

  • psql -U myuser -d mydb -c 'VACUUM (VERBOSE, ANALYZE) orders;'

    Clean up dead tuples and update statistics for the query planner

  • psql -U postgres -c "SELECT client_addr, state, sent_lsn, replay_lsn, (sent_lsn - replay_lsn) AS lag_bytes FROM pg_stat_replication;"

    Show replication state and lag in bytes for each replica

  • psql -U postgres -d mydb -c "SELECT pid, relation::regclass, mode, granted, left(query,60) FROM pg_locks l JOIN pg_stat_activity a USING(pid) WHERE relation IS NOT NULL ORDER BY granted;"

    Show table locks with their queries and whether each lock is granted or still waiting (waiting first)

  • SELECT pid, now()-query_start AS dur, state, query FROM pg_stat_activity WHERE state='active' AND now()-query_start > interval '5 min' ORDER BY dur DESC;

    List active queries that run longer than 5 minutes, longest first

  • SELECT pg_terminate_backend(<pid>); -- graceful first: SELECT pg_cancel_backend(<pid>);

    Kill a backend by pid; try pg_cancel_backend first, it cancels only the query

all 29 postgres commands