A foreign data wrapper is an extension available in PostgreSQL that allows you to consume data from different data sources not necessarily another PostgreSQL database system. Foreign data wrappers can serve all sorts of purposes:
- Completing a data flow cycle
- Your data may be segregated across databases, but still related in ways that makes being able to combine or aggregate it desirable
- Allows you to control the permissions on the foreign tables
- Improve performance and to check data integrity on microservice architecture
Steps to set up
- Set up in Local Server
- Set up in Remote server (Where actual data/table is located)
- Troubleshoot
- Add
postgres_fdw
extension
- Create two function
-
Handler with name
postgres_fdw_handler
with- Attribute
- Language -
c
- Return method -
simple
- Data type -
fwd_handler
- Language -
- Definition
- Symbol -
$libdir/postgres_fdw
- Library -
postgres_fdw_handler
- Symbol -
- Disable
SQL
code
- Attribute
-
Validator with name
postgres_fdw_validator
- Attribute
- Language -
c
- Return method -
simple
- Data type
void
- Language -
- Parameters
- Create parameter with
- name - p1
- type - text[]
- Create another parameter with
- name - p2
- type - oid
- Create parameter with
- Definition
- Symbol -
$libdir/postgres_fdw
- Library -
postgres_fdw_validator
- Symbol -
- Disable
SQL
code
- Attribute
-
-
Create FDW (Foreign Data Wrapper), with the above created
handler
andvalidator
.- name
postgres_fdw
- Select the above created
handler
fuction on the Handler &vallidator
function on Validator dropdown - Disable
SQL
code
- name
- Create
Foreign Server
with the following options- dbname -
postgres
- host -
127.0.0.1
- port -
5432
- dbname -
!> Please ensure that the remote database cluster
has the pg_hba.conf
entry corresponding to the database server for which the foreign server has to be created. Set up remote server
Debug with following SQL
select * from pg_foreign_server;
-
Click on
USER MAPPING
and create new mapping -
Select
server
and enter the user name and password in the options, withkey-value
pairuser : fdw_user password : thirumal
Debug with following query
select * from pg_user_mapping
- Create (OR) Import
foreign table
SQL
to import schema
IMPORT FOREIGN SCHEMA foreign_schema FROM SERVER foreign_server INTO public;
Create schema
with same name, if it's not available and create foreign table
with server
from the selection under New
-> schema
-
Create
read_only
user (Eg:fdw_user
) in theremote server
with enoughprivilage
on theschema/tables
. -
Check Role page
-
Give permission for foriegn server to access by modifying the configuration file
pg_hba.conf
that is located in/etc/postgresql/{version}/main
# IPv4 local connections:
host db_name fdw_user 127.0.0.1/32 scram-sha-256
Error:
ERROR: permission denied for table login_user
CONTEXT: remote SQL command: SELECT login_user_id, login_uuid, date_of_birth, individual, row_created_on FROM public.login_user
Solution:
- Check the role is added
- Check the permission for the role is added for the table