Connect node-postgres to a PostgreSQL-dialect database

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.

  1. 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.

  2. Specify localhost and 5432 as the database server host and port in the node-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