{"library":"pg-native","title":"pg-native: PostgreSQL Native Bindings","description":"`pg-native` provides high-performance native bindings for Node.js to PostgreSQL, leveraging the `libpq` C library for direct communication. As of version 3.7.0, it offers both asynchronous (callback-based) and synchronous API operations. Its key differentiators include superior performance compared to pure JavaScript alternatives, owing to its native implementation, and the unique provision of synchronous database interactions. While synchronous operations can be convenient for scripting or application bootstrapping, they are generally discouraged for non-blocking server environments due to their blocking nature. `pg-native` is part of the broader `node-postgres` ecosystem but requires `libpq` to be installed on the host system for compilation and runtime. The project demonstrates a healthy release cadence and active maintenance, with recent updates and community interaction.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pg-native"],"cli":null},"imports":["const Client = require('pg-native');","const { native } = require('pg');\nconst { Client } = native;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Client = require('pg-native');\n\nconst client = new Client();\nclient.connect(process.env.PG_CONNECTION_STRING ?? 'postgresql://user:password@localhost:5432/database', function(err) {\n  if(err) {\n    console.error('Connection error:', err.message);\n    return;\n  }\n  console.log('Connected to PostgreSQL database.');\n\n  // Text queries\n  client.query('SELECT NOW() AS the_date', function(err, rows) {\n    if(err) throw err;\n    console.log('Current date:', rows[0].the_date);\n\n    // Parameterized statements\n    client.query('SELECT $1::text as twitter_handle', ['@briancarlson'], function(err, rows) {\n      if(err) throw err;\n      console.log('Twitter handle:', rows[0].twitter_handle);\n    });\n  });\n\n  // Using prepared statements\n  client.prepare('get_twitter', 'SELECT $1::text as twitter_handle', 1, function(err) {\n    if(err) throw err;\n    client.execute('get_twitter', ['@briancarlson'], function(err, rows) {\n      if(err) throw err;\n      console.log('Prepared statement result 1:', rows[0].twitter_handle);\n\n      client.execute('get_twitter', ['@realcarrotfacts'], function(err, rows) {\n        if(err) throw err;\n        console.log('Prepared statement result 2:', rows[0].twitter_handle);\n        client.end(function() {\n          console.log('Client ended connection.');\n        });\n      });\n    });\n  });\n});","lang":"javascript","description":"This example demonstrates how to establish an asynchronous connection to a PostgreSQL database using `pg-native`, execute basic text queries, parameterized statements, and prepared statements, ensuring proper error handling and client termination.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}