{"id":10405,"library":"pg","title":"node-postgres","description":"node-postgres (pg) is a non-blocking PostgreSQL client for Node.js, offering both a pure JavaScript implementation and optional native libpq bindings with a unified API. As of version 8.20.0, it provides essential features like connection pooling, extensible data-type coercion, and support for parameterized queries, named statements, async notifications, and bulk import/export.","status":"active","version":"8.20.0","language":"javascript","source_language":"en","source_url":"git://github.com/brianc/node-postgres","tags":["javascript","database","libpq","pg","postgre","postgres","postgresql","rdbms"],"install":[{"cmd":"npm install pg","lang":"bash","label":"npm"},{"cmd":"yarn add pg","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"symbol":"Pool","correct":"import { Pool } from 'pg';"}],"quickstart":{"code":"import { Pool } from 'pg';\n\nasync function runQuery() {\n  // Ensure your POSTGRES_URL environment variable is set,\n  // e.g., 'postgresql://user:password@host:5432/database'\n  const pool = new Pool({\n    connectionString: process.env.POSTGRES_URL ?? 'postgresql://user:password@localhost:5432/testdb',\n  });\n\n  try {\n    const client = await pool.connect();\n    const res = await client.query('SELECT NOW() AS current_time');\n    console.log('Current time from PostgreSQL:', res.rows[0].current_time);\n    client.release(); // Release the client back to the pool\n  } catch (err) {\n    console.error('Database query failed', err);\n  } finally {\n    await pool.end(); // Close the pool when done with all operations\n  }\n}\n\nrunQuery();","lang":"typescript","description":"Connects to a PostgreSQL database using a connection pool, executes a simple `SELECT NOW()` query, logs the result, and properly releases the client and closes the pool."},"warnings":[{"fix":"Call `client.release()` when a client is no longer needed. For robust error handling, use a `try...finally` block to ensure release, or leverage `pool.connect().then(client => { ... }).finally(() => client.release())`.","message":"Always release client connections obtained from a pool to prevent resource exhaustion and application hangs.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use parameterized queries (e.g., `client.query('SELECT * FROM users WHERE id = $1', [userId])`) to safely pass user-provided values to the database.","message":"Concatenating user input directly into SQL queries can lead to severe SQL injection vulnerabilities.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For consistent time handling, consider configuring `pg.types.setTypeParser` to parse timestamps as strings or UTC Date objects, or ensure your application logic explicitly handles timezone conversions (e.g., storing and displaying all times in UTC).","message":"PostgreSQL `timestamp` and `timestamptz` types are returned as JavaScript `Date` objects, which can lead to timezone issues if not handled carefully, as JS `Date` objects are sensitive to the local timezone.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you intend to use the native bindings for potential performance benefits, explicitly install it: `npm install pg-native`.","message":"The `pg-native` package is an optional peer dependency. If it's not installed, `pg` will silently fall back to its pure JavaScript implementation, which may have different performance characteristics for some operations.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-04-18T00:00:00.000Z","next_check":"2026-07-17T00:00:00.000Z","problems":[{"fix":"Ensure your PostgreSQL server is running and configured to accept connections from your application's host. Check firewall rules and the database's `pg_hba.conf`.","cause":"The PostgreSQL server is not running or is inaccessible from the application's host and port.","error":"Error: connect ECONNREFUSED <host>:<port>"},{"fix":"Verify the `user` and `password` in your connection string or configuration object match the credentials configured on the PostgreSQL server.","cause":"The provided username or password for the database connection is incorrect.","error":"Error: password authentication failed for user \"<user>\""},{"fix":"Ensure the `database` name in your connection string or configuration object is correct and the database has been created on the PostgreSQL server.","cause":"The specified database name does not exist on the PostgreSQL server.","error":"Error: database \"<database_name>\" does not exist"},{"fix":"Ensure you do not attempt to use a `pg` client after calling `client.release()`. If you need to perform more queries, obtain a new client from the pool.","cause":"An attempt was made to use a `pg` client instance after it had already been released back to the connection pool.","error":"Error: Client was released and cannot be used to send queries anymore"},{"fix":"Carefully check the spelling and existence of the column name in your SQL query against your actual database schema.","cause":"A column referenced in your SQL query is not present in the specified table.","error":"error: column \"<column_name>\" does not exist"}],"ecosystem":"npm"}