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.
编辑:3年后(2014年3月),此常见问题条目已修订,更有帮助:
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.