将 node-postgres 连接到 PostgreSQL-dialect 数据库

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

  1. 确保 PGAdapter 与 使用 PostgreSQL node-postgres 驱动程序建立连接。

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

  2. 指定 localhost5432 作为数据库服务器主机和 node-postgres 连接属性。

    • (可选)如果 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();
    

后续步骤