How to Export PostgreSQL Database Without Data Using SQL?
Last Updated :
14 May, 2024
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
- Using pg_dump command
- 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.
Similar Reads
How to Export Database Schema Without Data in SQL? Database Schema specifies the structure of a database with its components like tables, columns, and indexes. Exporting data in SQL is an essential task in database management used to perform functions like data backup, recovery, migration, data analysis, performance optimization, compliance, auditin
4 min read
How to Export MySQL Database using Command Line? Exporting MySQL databases using the command line utility in Windows is a knowledge every Database Administrator and developer should possess. The mysqldump utility is an easy-to-use program that can back up databases, replicate or transfer data from one server to another and migrate databases. In th
4 min read
How to Dump and Restore PostgreSQL Database? PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
How to Export Schema Without Data in PL/SQL? In database management, there are times when you need to export the structure of your database objects such as tables, views, and procedures without including the data. This can be useful for creating backups, migrating databases, setting up development or testing environments, or sharing your schem
4 min read
How to Import and Export SQL Server Database? Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d
3 min read