postgresql_16:9.6.38 postgres_fdw — access data stored in external PostgreSQL servers
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > postgres_fdw — access data stored in external PostgreSQL servers
The postgres_fdw module provides the foreign-data wrapper postgres_fdw, which can be used to access data stored in external PostgreSQL servers.
The functionality provided by this module overlaps substantially with the functionality of the older (see postgresql_16:9.6.12) module. But postgres_fdw provides more transparent and standards-compliant syntax for accessing remote tables, and can give better performance in many cases.
To prepare for remote access using postgres_fdw: Install the postgres_fdw extension using (see postgresql_16:7.1.62). Create a foreign server object, using (see postgresql_16:7.1.80), to represent each remote database you want to connect to. Specify connection information, except user and password, as options of the server object. Create a user mapping, using (see postgresql_16:7.1.94), for each database user you want to allow to access each foreign server. Specify the remote user name and password to use as user and password options of the user mapping. Create a foreign table, using (see postgresql_16:7.1.64) or (see postgresql_16:7.1.149), for each remote table you want to access. The columns of the foreign table must match the referenced remote table. You can, however, use table and/or column names different from the remote table's, if you specify the correct remote names as options of the foreign table object.
Now you need only SELECT from a foreign table to access the data stored in its underlying remote table. You can also modify the remote table using INSERT, UPDATE, DELETE, COPY, or TRUNCATE. (Of course, the remote user you have specified in your user mapping must have privileges to do these things.)
Note that the ONLY option specified in SELECT, UPDATE, DELETE or TRUNCATE has no effect when accessing or modifying the remote table.
Note that postgres_fdw currently lacks support for INSERT statements with an ON CONFLICT DO UPDATE clause. However, the ON CONFLICT DO NOTHING clause is supported, provided a unique index inference specification is omitted. Note also that postgres_fdw supports row movement invoked by UPDATE statements executed on partitioned tables, but it currently does not handle the case where a remote partition chosen to insert a moved row into is also an UPDATE target partition that will be updated elsewhere in the same command.
It is generally recommended that the columns of a foreign table be declared with exactly the same data types, and collations if applicable, as the referenced columns of the remote table. Although postgres_fdw is currently rather forgiving about performing data type conversions at need, surprising semantic anomalies may arise when types or collations do not match, due to the remote server interpreting query conditions differently from the local server.
Note that a foreign table can be declared with fewer columns, or with a different column order, than its underlying remote table has. Matching of columns to the remote table is by name, not position.
Subsections
- postgresql_16:9.6.38.1 FDW Options of postgres_fdw
- postgresql_16:9.6.38.2 Functions
- postgresql_16:9.6.38.3 Connection Management
- postgresql_16:9.6.38.4 Transaction Management
- postgresql_16:9.6.38.5 Remote Query Optimization
- postgresql_16:9.6.38.6 Remote Query Execution Environment
- postgresql_16:9.6.38.7 Cross-Version Compatibility
- postgresql_16:9.6.38.8 Configuration Parameters
- postgresql_16:9.6.38.9 Examples
- postgresql_16:9.6.38.10 Author