Home Explore Blog CI



postgresql

20th chunk of `doc/src/sgml/postgres-fdw.sgml`
87de6536216ad39982baf2ee4c57a8dd7f970ac742ad79a90000000100000cf3
  <entry>User name on local server</entry>
        </row>
        <row>
         <entry><literal>%d</literal></entry>
         <entry>Database name on local server</entry>
        </row>
        <row>
         <entry><literal>%p</literal></entry>
         <entry>Process ID of backend on local server</entry>
        </row>
        <row>
         <entry><literal>%%</literal></entry>
         <entry>Literal %</entry>
        </row>
       </tbody>
      </tgroup>
     </informaltable>

     <para>
      For example, suppose user <literal>local_user</literal> establishes
      a connection from database <literal>local_db</literal> to
      <literal>foreign_db</literal> as user <literal>foreign_user</literal>,
      the setting <literal>'db=%d, user=%u'</literal> is replaced with
      <literal>'db=local_db, user=local_user'</literal>.
     </para>

    </listitem>
   </varlistentry>
  </variablelist>
 </sect2>

 <sect2 id="postgres-fdw-examples">
  <title>Examples</title>

  <para>
   Here is an example of creating a foreign table with
   <literal>postgres_fdw</literal>. First install the extension:
  </para>

<programlisting>
CREATE EXTENSION postgres_fdw;
</programlisting>

  <para>
   Then create a foreign server using <xref linkend="sql-createserver"/>.
   In this example we wish to connect to a <productname>PostgreSQL</productname> server
   on host <literal>192.83.123.89</literal> listening on
   port <literal>5432</literal>.  The database to which the connection is made
   is named <literal>foreign_db</literal> on the remote server:

<programlisting>
CREATE SERVER foreign_server
        FOREIGN DATA WRAPPER postgres_fdw
        OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db');
</programlisting>
  </para>

  <para>
   A user mapping, defined with <xref linkend="sql-createusermapping"/>, is
   needed as well to identify the role that will be used on the remote
   server:

<programlisting>
CREATE USER MAPPING FOR local_user
        SERVER foreign_server
        OPTIONS (user 'foreign_user', password 'password');
</programlisting>
  </para>

  <para>
   Now it is possible to create a foreign table with
   <xref linkend="sql-createforeigntable"/>.  In this example we
   wish to access the table named <structname>some_schema.some_table</structname>
   on the remote server.  The local name for it will
   be <structname>foreign_table</structname>:

<programlisting>
CREATE FOREIGN TABLE foreign_table (
        id integer NOT NULL,
        data text
)
        SERVER foreign_server
        OPTIONS (schema_name 'some_schema', table_name 'some_table');
</programlisting>

   It's essential that the data types and other properties of the columns
   declared in <command>CREATE FOREIGN TABLE</command> match the actual remote table.
   Column names must match as well, unless you attach <literal>column_name</literal>
   options to the individual columns to show how they are named in the remote
   table.
   In many cases, use of <link linkend="sql-importforeignschema"><command>IMPORT FOREIGN SCHEMA</command></link> is
   preferable to constructing foreign table definitions manually.
  </para>
 </sect2>

 <sect2 id="postgres-fdw-author">
  <title>Author</title>
  <para>
   Shigeru Hanada <email>shigeru.hanada@gmail.com</email>
  </para>
 </sect2>

</sect1>

Title: Postgres FDW Examples and Configuration
Summary
This section provides examples of how to use the postgres_fdw extension to create foreign tables and connect to remote PostgreSQL servers, including creating a foreign server, user mapping, and foreign table, as well as tips for ensuring data type and column name consistency between local and remote tables.