To import an SQL dump into a PostgreSQL database, follow these steps:
Prepare the SQL Dump File
First, ensure you have the SQL dump file you want to import. If you don't have one, you can create it from the source database using the pg_dump
command. For example, to create a dump of a database named "my_database," run the following in your terminal:
pg_dump -U your_username -d my_database > dump_file.sql
Replace your_username
with your PostgreSQL username, and dump_file.sql
with the desired name for the dump file.
Access PostgreSQL Command Line Interface
Next, you need to access the PostgreSQL Command Line Interface (CLI). Open your terminal and run the following command:
psql -U your_username -d your_database_name
Replace your_username
with your PostgreSQL username, and your_database_name
with the name of the target database where you want to import the SQL dump.
You will be prompted to enter your password after running the command.
Create a New Database (Optional)
If the target database does not exist, and you want to create it while importing the SQL dump, you can do so by running the following command within the PostgreSQL CLI:
CREATE DATABASE your_database_name;
Replace your_database_name
with the desired name for the new database.
Import the SQL Dump
With the PostgreSQL CLI open and the appropriate database selected, you can import the SQL dump using the following command:
\i /path/to/dump_file.sql
Replace /path/to/dump_file.sql
with the actual file path to your SQL dump file.
The SQL dump will be executed, and the data and schema will be imported into the specified PostgreSQL database.
Verify the Import (Optional)
After the import is completed, you may want to verify the data by running queries or accessing the data using a PostgreSQL client.
Exit PostgreSQL CLI
To exit the PostgreSQL CLI, simply type:
\q
0 Comment