
or you can use this: INSERT INTO TABLE1 (id, col1, col2, col3) SELECT id, 'data1', 'data2', 'data3' FROM TABLE2 WHERE cola 'something' Share. $dbh = DBI->connect($connection_string,'user','pass',Īnd then you can call the function inside database A: SELECT send_data( 'INSERT INTO jm (jm) VALUES (''zzzzzz'')' ) Īnd the value "zzzzzz" will be added into table "jm" in database B. CREATE TABLE newtable AS SELECT FROM existingtable WHERE condition The condition in the WHERE clause of the query defines which rows of the existing table will be copied to the new table. My $command = $_ || die 'No SQL command!' dblink allows you to easily fetch data from another database local or remote. We will look at two ways to do this using dblink and using copy table statement. You build a function in A like this: CREATE OR REPLACE FUNCTION send_data( VARCHAR ) Here are the steps to copy/transfer data from one database to another in PostgreSQL.
Postgres copy data from one table to another series#
Then PostgreSQL can join some Perl modules through series of the following commands at the end of nf for the database A: plperl.on_init='use DBI ' I use the procedural language plperlu (unsafe Perl procedural language) for it.ĭescription (all was done on a Linux server):Ĭreate plperlu language in your database A 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.Īctually, there is some possibility to send a table data from one PostgreSQL database to another. Postgres introduced "foreign data wrapper" in 9.1 (which was released after the question was asked). 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/). Graphweaver speeds up your GraphQL development by securely connecting all your data sources in one place. 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). 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 come from MySQL: what MySQL calls (loosely) " databases" are " schemas" in PostgreSQL - sort of namespaces. This will loop through all tables in the old schema and recreate them with data (no constraints, indexes, etc) in the new schema.- Set the search path to the target schema SET searchpath newSchema - Loop over the table names and recreate the tables DO DECLARE tablename text BEGIN FOR tablename IN SELECT t.tablename FROM informationschema.tables t WHERE t.tableschema 'public.

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.
