How to copy table from server to another in PostgreSQL?

Try this

using the COPY command from psql. Connect to server1 and export to CSV then connect to server2 and import from CSV


The safest way is to use pg_dump.

pg_dump --host server1 --encoding=utf8 --no-owner --username=foo --password -t table_name db_name > server1_db.sql
psql --host server2 --username=foo db_name -f server1_db.sql

exact commands -

1. Export via pg_dump into a file:

    pg_dump --host "source hostname" --port 5432 --username "username" --no-password --verbose --file "filename" --table "source schema.tablename" "source db name"

this will create a file called "filename" at the directory where you ran above command - that will have schema and data for the source table. You can can give any absolute path too.

2. Import via psql:

    psql --host "target hostname" --port 5432 --username "username" --password --verbose --file "file name" "target db name"  

->this will prompt for password

Before running import, drop the target table if exists.

Worked like charm and finished 10M rows within 2mins

Tags:

Postgresql