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.

Export PostgreSQL Database Without Data Using SQL

In PostgreSQL, exporting the database without data is a common and easy task that can be done with various approaches, Below are the approaches

  1. Using pg_dump command
  2. Using pg_dump command –schema-only option

1. 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.

2. pg_dump with –schema-only option

  • pg_dump is a PostgreSQL command with the –schema-only option used to extract the database schema, including tables, views, functions, and other database objects, without the data.
  • In the output, it creates an SQL script that can be helpful to recreate the database structure.

Syntax:

pg_dump --schema-only [options] dbname
  • –schema-only: This option tells pg_dump to exclude data and only dump the schema.
  • dbname: This is the name of the database you want to export.

Example: Exporting the gfg database schemas

Query:

prana>pg_dump -U postgres -h localhost -p 5432 -d gfg --schema-only > C:\Users\prana\OneDrive\Desktop\gfg_dump.sql

Explanation: After executing the pg_dump command with the provided options and –schema-only flag, a file named gfg_dump.sql (or any specified name) will be created in the directory specified (C:\Users\prana\OneDrive\Desktop). This file contains SQL statements to set up the database structure. Since the command includes the –schema-only option, it excludes data from the dump, focusing only on schema information.

Conclusion

In conclusion, exporting the database schema without data has multiple use cases such as maintaining version control of database structure, data migration, and sharing the data. The above approaches have their advantages & disadvantages according to your requirements choose the correct approach.

pg_dump command has more options that can be used in the export process. You can use the section option with other options, such as -n or —schema.


Contact Us