- Print
- DarkLight
Article summary
Did you find this summary helpful?
Thank you for your feedback
Postgres
How to export Postgres data to a SQL script
pg_dump -U your_username -d your_database_name --schema-only --table=your_table_name --column-inserts --file=/path/to/output/file.sql
Here's what each option does:
- -U your_username: specifies the username to use when connecting to the database.
- -d your_database_name: specifies the name of the database to connect to.
- --schema-only: dumps only the schema (tables, views, sequences, indexes, etc.) and not the data.
- --table=your_table_name: specifies the name of the table to dump. If you omit this option, all tables in the schema will be dumped.
- --column-inserts: generates INSERT statements that include column names, which can be useful for debugging.
- --file=/path/to/output/file.sql: specifies the name and path of the output file.
When you run this command, it will generate a SQL script that creates the schema and tables, along with their columns, in the output file.
Note that this command only extracts the schema and table structures, not the data. If you want to include the data as well, you can omit the --schema-only option. However, be careful when importing the data as it may overwrite existing data in the target database.