PostgreSQL: Export Schemas Only

Because every time you want to do it, you realize that you forgot how you did it the last time.

Only export the schema of a database

This will explort the schema of all of the tables with your database

# If your database is on the server/computer you are currently in, you can drop the -h hostname/IP
PGPASSWORD="your-db-password" pg_dump -h <hostname/IP> -U <username> -d <name-of-database> --schema-only > database_schema.sql

 

Only export the schema of a particular table

# This will only copy the schema of table_name
PGPASSWORD="your-password" pg_dump -h <hostname/IP> -U <username> -d <name-of-database> -t <table_name> --schema-only > table-schema.sql

Example with multiple tables

# This will only copy the schema of table1 and table2
PGPASSWORD="your-password" pg_dump -h <hostname/IP> -U <username> -d <name-of-database> -t <table1> -t <table2> --schema-only > tables-1-2-schema.sql