This article is half-done without your Comment! *** Please share your thoughts via Comment ***
The migration of data and tables between the server are a very common task for Database Administrator.
In this post, I am sharing few options to migrate your data between servers such a way that you do not require to create any additional table backup file.
Using pg_dump, SSH and PSQL, you can directly copy your table data from one server to another server.
Recently, I had taken PostgreSQL DBA interview and asked question like “How to migrate table data from one server to another in PostgreSQL?”
A candidate said, create table backup file using pg_dump -> copy that file in source server -> use pg_restore to restore it.
Don’t you think, so that above process is very long? Check the below few options, and you can migrate your data without any table backup file.
My Destination information:
- Host: pgtest1.dbrnd.com
- Table: public.tbl_stud
- User: anvesh
- Database: Students
My Source information:
- Host: pgtest2.dbrnd.com
- Table: public.tbl_stud
- User: anvesh
- Database: Students_test
Important Note:
Both the side table structure must be same like number columns and datatype of a column.
Option #1: Using SSH | PSQL
Use pg_dump with SSH | PSQL, and execute below command. It will ask password for pgtest2.dbrnd.com, and I set database password using variable PGPASSWORD. It will migrate only table data between server.
1 |
pg_dump --data-only --table=public.tbl_stud Students | ssh anvesh@pgtest2.dbrnd.com "PGPASSWORD=mypass psql -h pgtest2.dbrnd.com -U anvesh Students_test" |
Option #2: Using only PSQL (Transfer data only)
Option -a is for data only.
1 |
pg_dump -t public.tbl_stud -a Students | psql -h pgtest2.dbrnd.com -p 5432 -d Students_test |
Option #3: Using only PSQL (Transfer table structure with data)
1 |
pg_dump -t public.tbl_stud Students | psql -h pgtest2.dbrnd.com -p 5432 -d Students_test |
You can access below other related articles: