Database

postgres_fdw


The extension enables Postgres to query tables and views on a remote Postgres server.

Enable the extension

  1. Go to the Database page in the Dashboard.
  2. Click on Extensions in the sidebar.
  3. Search for "postgres_fdw" and enable the extension.

Create a connection to another database

1

Create a foreign server

Define the remote database address

1
2
3
4
5
6
7
create server "<foreign_server_name>" foreign data wrapper postgres_fdw options ( host '<host>', port '<port>', dbname '<dbname>' );
2

Create a server mapping

Set the user credentials for the remote server

1
2
3
4
5
6
create user mapping for "<dbname>"server "<foreign_server_name>"options ( user '<db_user>', password '<password>');
3

Import tables

Import tables from the foreign database

Example: Import all tables from a schema

1
2
3
import foreign schema "<foreign_schema>"from server "<foreign_server>"into "<host_schema>";

Example: Import specific tables

1
2
3
4
5
6
7
import foreign schema "<foreign_schema>"limit to ( "<table_name1>", "<table_name2>")from server "<foreign_server>"into "<host_schema>";
4

Query foreign table

1
select * from "<foreign_table>"

Configuring execution options

Fetch_size

Maximum rows fetched per operation. For example, fetching 200 rows with fetch_size set to 100 requires 2 requests.

1
2
alter server "<foreign_server_name>"options (fetch_size '10000');

Batch_size

Maximum rows inserted per cycle. For example, inserting 200 rows with batch_size set to 100 requires 2 requests.

1
2
alter server "<foreign_server_name>"options (batch_size '1000');

Extensions

Lists shared extensions. Without them, queries involving unlisted extension functions or operators may fail or omit references.

1
2
alter server "<foreign_server_name>"options (extensions 'vector, postgis');

For more server options, check the extension's official documentation

Resources