pg_dump command
pg_dump is a PostgreSQL command used to export an entire database. It can export the entire database, specific schemas, or individual database objects like tables, views, functions, and more.
Syntax:
pg_dump dbname
- dbname: This is the name of the database you want to export.
Example: Exporting the Database
Query:
pg_dump -U postgres -h localhost -p 5432 -d gfg > C:\Users\prana\OneDrive\Desktop\gfg_dump.sql
Explanation
- Use your PostgreSQL username instead of, hostname to indicate the server where the database resides, port for the database server (usually 5432), dbname to be your desired database name, and gfg_dump.sql to a file name you want for the database dump.
- After all the commands finish running, check that a file named gfg_dump.sql(or any other name you want) has been created inside this directory. It will be in the current directory. The data file saves the SQL statements to set up the database structure and insert the data.
How to Export PostgreSQL Database Without Data Using SQL?
When we are working with the PostgreSQL database, there are multiple times we need to export the database structure. This approach is useful when we create a skeleton database or migrate the schema changes for different environments or systems.
In this article, we will explore the process of exporting a database without data using SQL with the help of syntax and examples of each approach.