I need to transfer some data from another database. The old database is called paw1.moviesDB and the new database is paw1. The schema of each table are the following.
<You can not perform a cross-database query like SQL Server; PostgreSQL does not support this.
The DbLink extension of PostgreSQL is used to connect one database to another database. You have install and configure DbLink to execute a cross-database query.
I have already created a step-by-step script and example for executing cross database query in PostgreSQL. Please visit this post: PostgreSQL [Video]: Cross Database Queries using the DbLink Extension
From: hxxp://dbaspot.c om/postgresql/348627-pg_dump-t-give-where-condition.html (NOTE: the link is now broken)
# create temp table with the data
psql mydb
CREATE TABLE temp1 (LIKE mytable);
INSERT INTO temp1 SELECT * FROM mytable WHERE myconditions;
\q
# export the data to a sql file
pg_dump --data-only --column-inserts -t temp1 mtdb > out.sql
psql mydb
DROP TABLE temp1;
\q
# import temp1 rows in another database
cat out.sql | psql -d [other_db]
psql other_db
INSERT INTO mytable (SELECT * FROM temp1);
DROP TABLE temp1;
Another method useful in remotes
# export a table csv and import in another database
psql-remote> COPY elements TO '/tmp/elements.csv' DELIMITER ',' CSV HEADER;
$ scp host.com:/tmp/elements.csv /tmp/elements.csv
psql-local> COPY elements FROM '/tmp/elements.csv' DELIMITER ',' CSV;
I just had to do this exact thing so I figured I'd post the recipe here. This assumes that both databases are on the same server.
First, copy the table from the old db to the new db (because apparently you can't move data between databases). At the commandline:
pg_dump -U postgres -t <old_table> <old_database> | psql -U postgres -d <new_database>
# Just adding extra space here so scrollbar doesn't hide the command
Next, grant permissions of the copied table to the user of the new database. Log into psql:
psql -U postgres -d <new_database>
ALTER TABLE <old_table> OWNER TO <new_user>;
\q
Finally, copy data from the old table to the new table. Log in as the new user and then:
INSERT INTO <new_table> (field1, field2, field3)
SELECT field1, field2, field3 from <old_table>;
Done!