将 node-postgres 连接到 PostgreSQL 方言数据库

本页面介绍如何将 PostgreSQL node-postgres 驱动程序连接到 Spanner 中的 PostgreSQL 方言数据库。node-postgres 是适用于 PostgreSQL 的 Node.js 驱动程序。

  1. 确保 PGAdapter 与使用 PostgreSQL node-postgres 驱动程序连接的应用在同一机器上运行。

    如需了解详情,请参阅启动 PGAdapter

  2. node-postgres 连接属性中,将 localhost5432 指定为数据库服务器主机和端口。

    • (可选)如果 PGAdapter 配置为在默认 PostgreSQL 端口 (5432) 以外的端口上进行监听,则请指定其他端口号。
    • (可选)如果 PGAdapter 在不同于本地机器的主机上运行,请指定不同的主机名。
    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();
    

后续步骤