Yes, it is possible to work with multiple databases at the same time but you're looking in the wrong place. psycopg2 is just a library that simplifies accessing and manipulating data coming out of PostgreSQL but it doesn't go far beyond what you can do with psql. What you're looking to do you can solve on the database level by using Foreign Data Wrappers.
This does become more complicated in your schema definition but brings remote tables from host some.other.server
database remote_db
to appear as though they live on localhost
in database local_db
. A simple example on how to hook up the wrappers:
CREATE EXTENSION postgres_fdw;
CREATE SERVER some_remote_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'some.remote.server', port '5432', dbname 'remote_db');
CREATE USER MAPPING FOR local_user
SERVER some_remote_server
OPTIONS (user 'remote_user', password 'remote_user_password');
CREATE FOREIGN TABLE local_table_name (id int, value int)
SERVER some_remote_server
OPTIONS ( schema_name 'remote_schema_name', table_name 'remote_table_name');
Now locally you can just run
SELECT * from local_table_name
and the query will get executed against the remote host. Needless to say, this requires connectivity between the two servers.
Similarly, if you really have to, you can create a "remote" server against the localhost and point to a different local database for cross database queries. Feels dirty but it's possible.
As @a_horse_with_no_name mentioned though, this is not very efficient. If you find yourself doing this too frequently, you're not getting the most optimal performance and you'd better have very good reasons for keeping your databases separate at that point.
Best Answer
psycopg2
is a wrapper aroundlibpq
, written in C, to expose a Python DB-API compatible API to Python programs. It implements Python objects in C that calllibpq
functions. It has a thin Python module wrapper around it to load it and provide some of the interface functionality that's easier to write in pure Python.Anything that implements the DB-API to connect to an external database can be reasonably called a database driver or database adapter. The two are pretty much interchangeable. Any DB-API implementation must be a Python module. Python modules are programming language libraries, in that they're collections of re-usable code. Some are also C shared libraries, in that they're compiled C code that gets linked into the cPython executable.