將 node-postgres 連線至 PostgreSQL 方言資料庫

本頁說明如何將 PostgreSQL 節點 postgres 驅動程式連線至 Spanner 中的 PostgreSQL 方言資料庫。node-postgres 是 PostgreSQL 的 Node.js 驅動程式。

  1. 請確認 PGAdapter 與使用 PostgreSQL 節點 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();

後續步驟

  • 進一步瞭解 PGAdapter
  • 如要進一步瞭解 PostgreSQL node-postgres 驅動程式連線選項,請參閱 PGAdapter GitHub 存放區中的 node-postgres 連線選項