Native bindings between node.js & libpq are provided by the node-pg-native package. node-postgres can consume this package & use the native bindings to access the PostgreSQL server while giving you the same interface that is used with the JavaScript version of the library.
You need PostgreSQL client libraries & tools installed. An easy way to check is to type pg_config. If pg_config is in your path, you should be good to go. If it's not in your path you'll need to consult operating specific instructions on how to go about getting it there.
Some ways I've done it in the past:
- On macOS:
brew install libpq - On Ubuntu/Debian and Debian-based Node images:
apt-get install libpq-dev python3 g++ make - On RHEL/CentOS:
yum install postgresql-devel - On Windows:
- Install Visual Studio C++ (successfully built with Express 2010). Express is free.
- Install PostgreSQL (
http://www.postgresql.org/download/windows/) - Add your Postgre Installation's
binfolder to the system path (i.e.C:\Program Files\PostgreSQL\9.3\bin). - Make sure that both
libpq.dllandpg_config.exeare in that folder.
Install pg and pg-native them:
$ npm install pg pg-nativeOnce pg-native is installed instead of requiring a Client or Pool constructor from pg you do the following:
import pg from 'pg'
const { native } = pg
const { Client, Pool } = nativeWhen you access the .native property on 'pg' it will automatically require the pg-native package and wrap it in the same API.
Care has been taken to normalize between the two, but there might still be edge cases where things behave subtly differently due to the nature of using libpq over handling the binary protocol directly in JavaScript, so it's recommended you chose to either use the JavaScript driver or the native bindings both in development and production. For what its worth: I use the pure JavaScript driver because the JavaScript driver is more portable (doesn't need a compiler), and the pure JavaScript driver is plenty fast.
Some of the modules using advanced features of PostgreSQL such as pg-query-stream, pg-cursor,and pg-copy-streams need to operate directly on the binary stream and therefore are incompatible with the native bindings.