Table of Contents
pg_dump with -s
, -a
option is used on exporting only table scheme or data.
Export table scheme and data separately.
Export Table Scheme
1 |
pg_dump DB_NAME -h HOSTNAME -t TABLE_NAME -U USER_NAME -s --file=OUTPUT_FILE_NAME |
You can use this when you want to create the database similar to another.
If you want to all table schemes remove -t TABLE_NAME
.
Export Table Data
1 |
pg_dump DB_NAME -h HOSTNAME -t TABLE_NAME -U USER_NAME -a --file=OUTPUT_FILE_NAME |
Commands may also change, so please refer to the PostgreSQL documentation for the most up-to-date information.
To import the exported data, execute psql
with the -f file_name
option.
1 |
psql DB_NAME -U user_name -W -f FILE_NAME |
I use above commands when I add a new column to existing table.
Add Column
PostgreSQL allows us to add new column only after existing columns. On the other hand, MySQL allows us to add new column wherever position.
So I add column in PostgreSQL in the following procedure.
Export table scheme and data separately. (You can also export both data and schema together.)
12pg_dump DBNAME -h HOSTNAME -t TABLENAME -U USERNAME -a --file=20130212_data.sqlpg_dump DBNAME -h HOSTNAME -t TABLENAME -U USERNAME -s --file=20130212_schema.sqlChange the exported table scheme, add table deletion sql on the top.
1vim 20130212_schema.sqlCreate backup.
1pg_dump DBNAME -h HOSTNAME -t TABLENAME -U USERNAME --file=20130212_backup.sqlChange table and data.
12psql DBNAME -h HOSTNAME -U USERNAME -f 20130212_schema.sqlpsql DBNAME -h HOSTNAME -U USERNAME -f 20130212_data.sql