Menghubungkan node-postgres ke database dialek PostgreSQL
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara menghubungkan driver node-postgres PostgreSQL ke database berdialek PostgreSQL di Spanner. node-postgres adalah driver Node.js untuk PostgreSQL.
Pastikan PGAdapter berjalan di mesin yang sama dengan
aplikasi yang terhubung menggunakan driver node-postgres PostgreSQL.
Untuk mengetahui informasi selengkapnya, lihat Mulai PGAdapter.
Tentukan host dan port server database di properti koneksi node-postgres:
const{Client}=require('pg');constclient=newClient({host:'APPLICATION_HOST',port:PORT,database:'DATABASE_NAME',});awaitclient.connect();constres=awaitclient.query("select 'Hello world!' as hello");console.log(res.rows[0].hello);awaitclient.end();
Ganti kode berikut:
APPLICATION_HOST: nama host atau alamat IP
mesin tempat PGAdapter berjalan. Jika menjalankan secara lokal, Anda dapat menggunakan localhost.
PORT: nomor port tempat PGAdapter
berjalan. Ubah ini di string koneksi jika PGAdapter berjalan di port kustom. Jika tidak, gunakan port default, 5432.
Soket domain Unix
Bagian ini menjelaskan cara menggunakan soket domain Unix untuk menghubungkan driver node-postgres PostgreSQL ke database dialek PostgreSQL. Gunakan koneksi soket domain Unix jika Anda memerlukan latensi serendah mungkin.
Untuk menggunakan soket domain Unix, PGAdapter harus berjalan di host yang sama dengan aplikasi klien.
constclient=newClient({host:'/tmp',port:PORT,database:'DATABASE_NAME',});awaitclient.connect();constres=awaitclient.query("select 'Hello world!' as hello");console.log(res.rows[0].hello);awaitclient.end();
Ganti kode berikut:
/tmp: direktori soket domain default untuk
PGAdapter. Setelan ini dapat diubah menggunakan argumen command line -dir.
PORT: nomor port tempat PGAdapter
berjalan. Ubah ini di string koneksi jika PGAdapter berjalan di port kustom. Jika tidak, gunakan port default, 5432.
Untuk mengetahui informasi selengkapnya tentang opsi koneksi driver node-postgres PostgreSQL, lihat Opsi Koneksi node-postgres di repositori GitHub PGAdapter.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-11 UTC."],[],[],null,["# Connect node-postgres to a PostgreSQL-dialect database\n\nThis page explains how to connect the PostgreSQL node-postgres driver to a\nPostgreSQL-dialect database in Spanner. node-postgres is a Node.js\ndriver for PostgreSQL.\n\n1. Verify that PGAdapter is running on the same machine as the\n application that is connecting using the PostgreSQL node-postgres driver.\n\n For more information, see [Start PGAdapter](/spanner/docs/pgadapter-start).\n2. Specify the database server host and port in the\n `node-postgres` connection properties:\n\n const { Client } = require('pg');\n const client = new Client({\n host: '\u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e',\n port: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-kt\"\u003ePORT\u003c/span\u003e\u003c/var\u003e,\n database: '\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e',\n });\n await client.connect();\n const res = await client.query(\"select 'Hello world!' as hello\");\n console.log(res.rows[0].hello);\n await client.end();\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e: the hostname or IP address of the machine where PGAdapter is running. If running locally, you can use `localhost`.\n - \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e: the port number where PGAdapter is running. Change this in the connection string if PGAdapter is running on a custom port. Otherwise, use the default port, `5432`.\n\nUnix domain sockets\n-------------------\n\nThis section explains how to use Unix domain sockets to connect a\nPostgreSQL node-postgres driver to a PostgreSQL-dialect database. Use Unix domain socket\nconnections when you need to have the lowest possible latency.\n\nTo use Unix domain sockets, PGAdapter must be running on the\nsame host as the client application. \n\n const client = new Client({\n host: '\u003cvar translate=\"no\"\u003e/tmp\u003c/var\u003e',\n port: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-kt\"\u003ePORT\u003c/span\u003e\u003c/var\u003e,\n database: '\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e',\n });\n await client.connect();\n const res = await client.query(\"select 'Hello world!' as hello\");\n console.log(res.rows[0].hello);\n await client.end();\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003e/tmp\u003c/var\u003e: the default domain socket directory for PGAdapter. This can be changed using the `-dir` command line argument.\n- \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e: the port number where PGAdapter is running. Change this in the connection string if PGAdapter is running on a custom port. Otherwise, use the default port, `5432`.\n\nWhat's next\n-----------\n\n- Learn more about [PGAdapter](/spanner/docs/pgadapter).\n- For more information about PostgreSQL node-postgres driver connection options, see [node-postgres Connection\n Options](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/docs/node-postgres.md) in the PGAdapter GitHub repository."]]