Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining Results from Two Separate Databases

Is it possible to JOIN rows from two separate postgres databases?

I am working with system with couple databases in one server and sometimes I really need such a feature.

like image 736
sennin Avatar asked Jan 13 '11 10:01

sennin


People also ask

Can you join 2 tables from different databases?

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully qualify table names.

How do I link two databases together?

The Get External Data - Access Database import and link wizard opens. In the File name text box, type the name of the source database or click Browse to display the File Open dialog box. Click Link to the data source by creating a linked table, and then click OK. The Link Tables dialog box opens.

Can we join tables from two different databases MySQL?

Yes, assuming the account has appropriate permissions you can use: SELECT <...> FROM A. table1 t1 JOIN B.

Can we join tables from two different schemas?

yes you can go for it. Need to write the schema name before the table. select Grant premission on it. Source qualifier can join tables from same db with different schemas.


1 Answers

According to http://wiki.postgresql.org/wiki/FAQ

There is no way to query a database other than the current one. Because PostgreSQL loads database-specific system catalogs, it is uncertain how a cross-database query should even behave. contrib/dblink allows cross-database queries using function calls. Of course, a client can also make simultaneous connections to different databases and merge the results on the client side.

EDIT: 3 years later (march 2014), this FAQ entry has been revised and is more helpful:

How do I perform queries using multiple databases?

There is no way to directly query a database other than the current one. Because PostgreSQL loads database-specific system catalogs, it is uncertain how a cross-database query should even behave.

The SQL/MED support in PostgreSQL allows a "foreign data wrapper" to be created, linking tables in a remote database to the local database. The remote database might be another database on the same PostgreSQL instance, or a database half way around the world, it doesn't matter. postgres_fdw is built-in to PostgreSQL 9.3 and includes read/write support; a read-only version for 9.2 can be compiled and installed as a contrib module.

contrib/dblink allows cross-database queries using function calls and is available for much older PostgreSQL versions. Unlike postgres_fdw it can't "push down" conditions to the remote server, so it'll often land up fetching a lot more data than you need.

Of course, a client can also make simultaneous connections to different databases and merge the results on the client side.

like image 75
ndtreviv Avatar answered Sep 22 '22 20:09

ndtreviv