|
375 | 375 | </para>
|
376 | 376 | </sect2>
|
377 | 377 |
|
| 378 | + <sect2> |
| 379 | + <title>Examples</title> |
| 380 | + |
| 381 | + <para> |
| 382 | + Here is an example of creating a foreign table with |
| 383 | + <literal>postgres_fdw</>. First install the extension: |
| 384 | + </para> |
| 385 | + |
| 386 | +<programlisting> |
| 387 | +CREATE EXTENSION postgres_fdw; |
| 388 | +</programlisting> |
| 389 | + |
| 390 | + <para> |
| 391 | + Then create a foreign server using <xref linkend="sql-createserver">. |
| 392 | + In this example we wish to connect to a <productname>PostgreSQL</> server |
| 393 | + on host <literal>192.83.123.89</literal> listening on |
| 394 | + port <literal>5432</literal>. The database to which the connection is made |
| 395 | + is named <literal>foreign_db</literal> on the remote server: |
| 396 | + |
| 397 | +<programlisting> |
| 398 | +CREATE SERVER foreign_server |
| 399 | + FOREIGN DATA WRAPPER postgres_fdw |
| 400 | + OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db'); |
| 401 | +</programlisting> |
| 402 | + </para> |
| 403 | + |
| 404 | + <para> |
| 405 | + A user mapping, defined with <xref linkend="sql-createusermapping">, is |
| 406 | + needed as well to identify the role that will be used on the remote |
| 407 | + server: |
| 408 | + |
| 409 | +<programlisting> |
| 410 | +CREATE USER MAPPING FOR local_user |
| 411 | + SERVER foreign_server |
| 412 | + OPTIONS (user 'foreign_user', password 'password'); |
| 413 | +</programlisting> |
| 414 | + </para> |
| 415 | + |
| 416 | + <para> |
| 417 | + Now it is possible to create a foreign table with |
| 418 | + <xref linkend="sql-createforeigntable">. In this example we |
| 419 | + wish to access the table named <structname>some_schema.some_table</> |
| 420 | + on the remote server. The local name for it will |
| 421 | + be <structname>foreign_table</>: |
| 422 | + |
| 423 | +<programlisting> |
| 424 | +CREATE FOREIGN TABLE foreign_table ( |
| 425 | + id serial NOT NULL, |
| 426 | + data text |
| 427 | +) |
| 428 | + SERVER foreign_server |
| 429 | + OPTIONS (schema_name 'some_schema', table_name 'some_table'); |
| 430 | +</programlisting> |
| 431 | + |
| 432 | + It's essential that the data types and other properties of the columns |
| 433 | + declared in <command>CREATE FOREIGN TABLE</> match the actual remote table. |
| 434 | + Column names must match as well, unless you attach <literal>column_name</> |
| 435 | + options to the individual columns to show how they are named in the remote |
| 436 | + table. |
| 437 | + </para> |
| 438 | + </sect2> |
| 439 | + |
378 | 440 | <sect2>
|
379 | 441 | <title>Author</title>
|
380 | 442 | <para>
|
|
0 commit comments