dblink Functions

dblink Functions

The dblink module is provided for making easy connections to other databases either on the same database host, or on a remote host. dblink is intended for database users to perform short ad hoc queries in other databases. dblink is not intended as a replacement for external tables or for administrative tools such as gptransfer.

The following procedure shows the basic steps for configuring and using dblink in Greenplum Database. See dblink in the PostgreSQL documentation for more information about individual functions.

  1. Begin by creating a sample table to query using the dblink functions. These commands create a small table in the postgres database, which you will later query from the gpadmin database using dblink:
    $ psql -d postgres
    psql (8.3.23)
    Type "help" for help.
    
    postgres=# CREATE TABLE testdblink (a int, b text) DISTRIBUTED BY (a);
    CREATE TABLE
    postgres=# INSERT INTO testdblink VALUES (1, 'Cheese');
    INSERT 0 1
    postgres=# INSERT INTO testdblink VALUES (2, 'Fish');
    INSERT 0 1
    postgres=# \q
    $
  2. Log into a different database (gpadmin in this example) and install the dblink functions if they are not already available. You install the dblink functions using the $GPHOME/share/postgresql/contrib/dblink.sql script:
    $ psql -d gpadmin
    psql (8.3.23)
    Type "help" for help.
    
    gpadmin=# \i /usr/local/gpdb/share/postgresql/contrib/dblink.sql
    SET
    CREATE FUNCTION
    CREATE FUNCTION
    CREATE FUNCTION
    CREATE FUNCTION
    REVOKE
    REVOKE
    CREATE FUNCTION
    CREATE FUNCTION
    ...
  3. Use the dblink_connect function to create both implicit and named connections to other databases. The connection string that you provide should be a libpq-style keyword/value string. For example, to create a named connection to the postgres database on the local Greenplum Database system:
    gpadmin=# SELECT dblink_connect('mylocalconn', 'dbname=postgres');
     dblink_connect
    ----------------
     OK
    (1 row)
    To make a connection to a remote database system, simply include host and port information in the connection string. For example, to create an implicit dblink connection to a remote system:
    gpadmin=# SELECT dblink_connect('host=remotehost port=5432 dbname=postgres');
  4. Use the basic dblink function to query a database using a configured connection. Keep in mind that the dblink function returns a record type, so you must assign the columns returned in the dblink query. For example, the following command uses the named connection to query the table you created in Step 1:
    gpadmin=# SELECT * FROM dblink('mylocalconn', 'SELECT * FROM testdblink') AS dbltab(id int, product text);
     id | product
    ----+---------
      1 | Cheese
      2 | Fish
    (2 rows)
    In this release of Greenplum Database, statements that modify table data cannot use named or implicit dblink connections. Instead, you must provide the connection string directly in the dblink function. For example:
    gpadmin=# CREATE TABLE testdbllocal (a int, b text) DISTRIBUTED BY (a);
    CREATE TABLE
    gpadmin=# INSERT INTO testdbllocal select * FROM dblink('dbname=postgres', 'SELECT * FROM testdblink') AS dbltab(id int, product text);
    INSERT 0 2