Backing Up and Restoring PostgreSQL Databases
By Ercan - 17/04/2025 - 0 comments
This guide outlines the steps to back up and restore PostgreSQL databases using command-line tools on Windows.
Backing Up a Database
To back up a PostgreSQL database, use the `pg_dump` utility:
.\pg_dump.exe -U <username> -d <database_name> > <dump_path>
Example:
.\pg_dump.exe -U postgres -d testdb > C:\Users\Ercan\Desktop\db_dump.sql
Backing Up a Table
To back up a PostgreSQL table, use the `pg_dump` utility:
.\pg_dump.exe -U <username> -d <database_name> -t <table_name> > <dump_path>
Example:
.\pg_dump.exe -U postgres -d testdb -t testtable > C:\Users\Ercan\Desktop\table_dump.sql
Restoring a Database or Table
To restore a PostgreSQL database from a backup, use the `psql` utility:
.\psql.exe -U <username> -d <database_name> < <dump_path>
Example:
.\psql.exe -U postgres -d testdb < C:\Users\Ercan\Desktop\db_or_table_dump.sql
Ensure that the specified user has the necessary privileges to perform backup and restore operations.
Note: You must use Command Prompt for the above commands to work.
Tags: postgresql, dump, restore