2

I have two POSTGRES databases stored in different servers.

The "Firstdb" is version 9.2 and it is stored in a LAN server, port 5432.

The "Seconddb" is version 10 and it is stored as localhost to my PC, port 5432.

I have access to both of them through pgAdmin4 version 2.0.

I would like to run query between those two databases to compare data.

Any ideas about how this can be done?

Thank you all for your time.

1
  • 2
    Use a foreign data wrapper Commented Mar 29, 2018 at 10:48

1 Answer 1

2

For running federated queries I use most of the time postgres_fdw, which creates a foreign table in the source database. Quite handy but has its caveats when dealing with joins.

An example:

CREATE SERVER my_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'target.host.com', port '5432', dbname 'targetdb');
CREATE USER MAPPING FOR postgres SERVER my_server OPTIONS (user 'postgres');

CREATE FOREIGN TABLE my_foreign_table (
  id INT,
  desc TEXT
)
SERVER my_server OPTIONS (schema_name 'public', table_name 'target_table');

EDIT based on the comments:

Keep in mind that the source database, as any other application, needs access to the target database and it has to be described at the pg_hba.conf:

host yourdb youruser 0.0.0.0 md5

Another approach is using dblink, which does not create a foreign table but enables you to fire queries directly to the target database and retrieve the result sets just as if it was local.

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

4 Comments

I submited your code successfully. The foreign table created to my "Seconddb". But, if I query select * from foreign_table I get this result: ERROR: could not connect to server "my_server" DETAIL: FATAL: no pg_hba.conf entry for host "xxx.xxx.xxx.xxx", user "postgres", database "nsw", SSL off SQL state: 08001
That's a whole other problem you have now. You user has no access to the target database. You have to edit the pg_hba.conf file in the target database and add the missing credentials.
At the pg_hba.conf in the target server add something like: host nsw postgres xxx.xxxx.xxxx.xxxx trust. Keep in mind that there are other options for trust: see postgresql.org/docs/9.1/static/auth-pg-hba-conf.html
I edited my answer with the last bit regarding the pg_hba.conf

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.