Transfer data between databases with PostgreSQL

前端 未结 9 1516
我寻月下人不归
我寻月下人不归 2020-12-22 17:51

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.

<
9条回答
  •  清歌不尽
    2020-12-22 18:00

    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):

    1. Create plperlu language in your database A

    2. 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;'
      
    3. 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.

提交回复
热议问题