Connect to and query a remote PostgreSQL database from a local database.

First install the dblink extension and enable it for your database, e.g.

sudo apt-get install postgresql-contrib-9.1
sudo -u postgres psql -c 'CREATE EXTENSION dblink;' foo

Then connect to the remote database with the dblink_connect function:

dblink_connect('hostaddr= port=5432 dbname=foo user=bar password=baz');