Copy a table from one database to another in Postgres

nix picture nix · Jul 7, 2010 · Viewed 242k times · Source

I am trying to copy an entire table from one database to another in Postgres. Any suggestions?

Answer

thomax picture thomax · May 23, 2013

Extract the table and pipe it directly to the target database:

pg_dump -t table_to_copy source_db | psql target_db

Note: If the other database already has the table set up, you should use the -a flag to import data only, else you may see weird errors like "Out of memory":

pg_dump -a -t my_table my_db | psql target_db