4

I know that in Oracle it's possible to create stored dblink and after that use it in query. For example:

Script for creation dblink:

CREATE PUBLIC DATABASE LINK my_link CONNECT TO my_schema IDENTIFIED BY shema_password USING 'remote';

And after that we can use it in our queries:

SELECT * FROM some_table@my_link;

I didn't find same solution for Postgres. I undestood that we can create named dblink connection:

For this we must use dblink_connect with name param. But created named dblink will destroy after session close.

Or we can create dblink connection for every queries:

SELECT *
FROM dblink('host= port= dbname= user= password=',
            'select table_schema, table_name from information_schema.tables where table_schema = ''data''') AS t1 (table_schema TEXT, table_name TEXT);

Is it possible create stored dblink in Postgres and use it in different queries? Or I should create some function that return dblink connection params which encapsulate them?

I try use foreign table and do next steps:

Create postgres_fdw extension:

CREATE EXTENSION IF NOT EXISTS postgres_fdw;

Create Server:

CREATE SERVER my_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '-', port '-', dbname '-');

And create mapping for user 'sys' where set remote user and password:

CREATE USER MAPPING FOR sys SERVER light_house OPTIONS ( USER 'remote_user', PASSWORD 'remove_password');

GRANT USAGE ON FOREIGN SERVER my_server TO sys;

Create foreign table in schema:

CREATE FOREIGN TABLE system.my_local_table (
  colums ..
) SERVER my_server OPTIONS (schema_name 'remote_user', table_name 'remote_table'
);

GRANT SELECT ON TABLE system.home_measurement TO argus_sys;

after that I catch next exception:

[2F003] ERROR: password is required 
Description: Non-superuser cannot connect if the server does not request a password. 
Help: Target server's authentication method must be changed.
3
  • "Non-superuser cannot connect if the server does not request a password" - you honestly run a production server that does not require a password to connect? Commented Jan 23, 2017 at 17:15
  • @a_horse_with_no_name sorry, but I don't undestand. I declare passord to remote user in user mapping (I updated question and added scripts) and I thing that it's enough or I wrong? Commented Jan 24, 2017 at 6:31
  • The error message seems to indicate that your server does not require a password. Commented Jan 24, 2017 at 6:48

1 Answer 1

2

You should use a foreign table.

To get rid of the error message, change the pg_hba.conf file on the remote database server to use md5 authentication (don't forget to reload with pg_ctl reload).

Sign up to request clarification or add additional context in comments.

3 Comments

I try create foreign table but I can't use it with non superuser. I updated my question, please look it.
When you say: "foreign server" you mean "remote db instance", right? There I must add configuration for my "local db instance" for example: host remote_db_name remote_user_name local_ip md5
Right, that's what I mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.