This page explains how to connect the PostgreSQL node-postgres driver to a PostgreSQL-dialect database in Spanner. node-postgres is a Node.js driver for PostgreSQL.
Ensure that PGAdapter is running on the same machine as the application that is connecting using the PostgreSQL node-postgres driver.
For more information, see Start PGAdapter.
Specify
localhost
and5432
as the database server host and port in thenode-postgres
connection properties.- Optionally specify a different port number if PGAdapter is configured to listen on a port other than the default PostgreSQL port (5432).
- Optionally specify a different host name if PGAdapter is running on a different host than the local machine.
const { Client } = require('pg'); const client = new Client({ host: 'localhost', port: 5432, database: 'my-database', }); await client.connect(); const res = await client.query("select 'Hello world!' as hello"); console.log(res.rows[0].hello); await client.end();
What's next
- Learn more about PGAdapter.
- For more information about PostgreSQL node-postgres driver connection options, see node-postgres Connection Options in the PGAdapter GitHub repository.