Search code examples
djangopostgresqldjango-modelspsqlpg-dump

How to pg_dump only data from a table in a PostgreSQL database and pipe it into an identical but differently-named table in another database


Goal

I need to copy just the data from a table in one database into another table in another database. The table in the target database has the exact same schema except that the name of the table is different. The two databases are on the same machine.

Problem

I can't find a way to pipe the data from pg_dump into a differently named table in the target database.

What I've tried

Here's the command I would use if the tables were named the exact same:

pg_dump -a -t <table_name> <source_db> | psql <target_db>

But where would I be able to put the name of the target table in the target database?

Background info

The reason I'm doing this is because I'm building a Django web-app, and I've realized I really need to separate a bunch of the models I've created into different 'apps' within my Django project.


Solution

  • You could do it with COPY:

    psql -d db1 -c 'COPY table_1 TO STDOUT' | psql -d db2 -c 'COPY table_2 FROM STDIN'