PostgresSQL Cross-Database Query Solution
Solution 1: Use Schemas Instead of Separate Databases (Recommended) If you control the database design and these tables need to talk to each other frequently, they probably shouldn't be in separate databases. Instead, move them into different schemas within the same database . In Postgres, you can easily query across schemas using standard dot notation: SQL SELECT * FROM current_database.other_schema.your_table; -- Or simply: SELECT * FROM other_schema.your_table; Solution 2: Use postgres_fdw (Foreign Data Wrapper) If you absolutely must keep the databases separate, the official and most efficient way to connect them is using the postgres_fdw extension. This makes a table from another database look and act like a local table. Here is how to set it up in the database you are currently connected to: 1. Enable the extension SQL CREATE EXTENSION postgres_fdw; 2. Create the foreign server connection SQL CREATE SERVER foreign_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS...