Introduction: Restoring a PostgreSQL database from a backup is a common task for database administrators and developers. This guide provides a step-by-step process to restore a PostgreSQL database using an SQL dump file. Request: • PostgreSQL is installed on your system. • Access to command line interface (CLI). • An SQL dump file (in this case, backup.sql). • Appropriate permissions to access and restore the database.
Open Your Command Line Interface: Start by opening your CLI. This can be Terminal on macOS/Linux or Command Prompt/Powershell on Windows.
Navigate to the Directory Containing the Backup File: If your backup file (backup.sql) is not in the root directory, use the cd command to navigate to the directory where it is stored. For example, if it was in the /backup directory, you would type cd /backup.
Log in to PostgreSQL: Use the psql command line utility to log in to your PostgreSQL server. The basic command format is psql -U [username] -d [database], where [username] is your PostgreSQL username and [database] is the database you want restore. In this case, the command is:
psql -U postgres -d restore -f /backup/backup.sql
sudo -u postgres psql
\c restore
SELECT * FROM clients;