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.
<Note:- Sourcedb already exists in your database.
CREATE DATABASE targetdb WITH TEMPLATE sourcedb;
This statement copies the sourcedb to the targetdb.
Step 1:- Dump the source database to a file.
pg_dump -U postgres -O sourcedb sourcedb.sql
Note:- Here postgres is the username so change the name accordingly.
Step 2:- Copy the dump file to the remote server.
Step 3:- Create a new database in the remote server
CREATE DATABASE targetdb;
Step 4:- Restore the dump file on the remote server
psql -U postgres -d targetdb -f sourcedb.sql
(pg_dump is a standalone application (i.e., something you run in a shell/command-line) and not an Postgres/SQL command.)
This should do it.
Actually, there is some possibility to send a table data from one PostgreSQL database to another. I use the procedural language plperlu (unsafe Perl procedural language) for it.
Description (all was done on a Linux server):
Create plperlu language in your database A
Then PostgreSQL can join some Perl modules through series of the following commands at the end of postgresql.conf for the database A:
plperl.on_init='use DBI;'
plperl.on_init='use DBD::Pg;'
You build a function in A like this:
CREATE OR REPLACE FUNCTION send_data( VARCHAR )
RETURNS character varying AS
$BODY$
my $command = $_[0] || die 'No SQL command!';
my $connection_string =
"dbi:Pg:dbname=your_dbase;host=192.168.1.2;port=5432;";
$dbh = DBI->connect($connection_string,'user','pass',
{AutoCommit=>0,RaiseError=>1,PrintError=>1,pg_enable_utf8=>1,}
);
my $sql = $dbh-> prepare( $command );
eval { $sql-> execute() };
my $error = $dbh-> state;
$sql-> finish;
if ( $error ) { $dbh-> rollback() } else { $dbh-> commit() }
$dbh-> disconnect();
$BODY$
LANGUAGE plperlu VOLATILE;
And then you can call the function inside database A:
SELECT send_data( 'INSERT INTO jm (jm) VALUES (''zzzzzz'')' );
And the value "zzzzzz" will be added into table "jm" in database B.
Databases are isolated in PostgreSQL; when you connect to a PostgreSQL server you connect to just one database, you can't copy data from one database to another using a SQL query.
If you come from MySQL: what MySQL calls (loosely) "databases" are "schemas" in PostgreSQL - sort of namespaces. A PostgreSQL database can have many schemas, each one with its tables and views, and you can copy from one schema to another with the schema.table
syntax.
If you really have two distinct PostgreSQL databases, the common way of transferring data from one to another would be to export your tables (with pg_dump -t ) to a file, and import them into the other database (with psql
).
If you really need to get data from a distinct PostgreSQL database, another option - mentioned in Grant Johnson's answer - is dblink, which is an additional module (in contrib/
).
Update:
Postgres introduced "foreign data wrapper" in 9.1 (which was released after the question was asked). Foreign data wrappers allow the creation of foreign tables through the Postgres FDW which makes it possible to access a remote table (on a different server and database) as if it was a local table.
This worked for me to copy a table remotely from my localhost to Heroku's postgresql:
pg_dump -C -t source_table -h localhost source_db | psql -h destination_host -U destination_user -p destination_port destination_db
This creates the table for you.
For the other direction (from Heroku to local)
pg_dump -C -t source_table -h source_host -U source_user -p source_port source_db | psql -h localhost destination_db
Just like leonbloy suggested, using two schemas in a database is the way to go. Suppose a source schema (old DB) and a target schema (new DB), you can try something like this (you should consider column names, types, etc.):
INSERT INTO target.Awards SELECT * FROM source.Nominations;
There are three options for copying it if this is a one off:
If this is an ongoing need, the answers are: