How to export table structure in PostgreSQL database?

To export the table structure in a PG database, you can use the following command:

  1. Export the entire database structure using the pg_dump command.
pg_dump -s -U username -d database_name -f output_file.sql

The -s option signifies exporting only the structure, while the -U option specifies the database username, the -d option specifies the database name to be exported, and the -f option specifies the path and name of the output file.

  1. Export the structure of a specific table using the pg_dump command.
pg_dump -t table_name -s -U username -d database_name -f output_file.sql

The -t option specifies the name of the table to export.

  1. Export the structure of the entire database cluster using the pg_dumpall command.
pg_dumpall -s -U username -f output_file.sql

The “-s” option indicates exporting only the structure.

The exported file will contain the complete table structure definition, including table names, column names, data types, constraints, indexes, and other information.

bannerAds