nodejs postgresql local connection
const pgp = require('pg-promise')(/* initialization options */); const cn = { host: 'localhost', // server name or IP address; port: 5432, database: 'myDatabase', user: 'myUser', password: 'myPassword' }; // alternative: // var cn = 'postgres://username:password@host:port/database'; const db = pgp(cn); // database instance; // select and return a single user name from id: db.one('SELECT name FROM users WHERE id = $1', [123]) .then(user => { console.log(user.name); // print user name; }) .catch(error => { console.log(error); // print the error; }); // alternative - new ES7 syntax with 'await': // await db.one('SELECT name FROM users WHERE id = $1', [123]);
Here is what the above code is Doing:
1. It creates a database instance, using the connection string.
2. It executes a query, and returns a single user name from id.
3. It prints the user name, or the error.
The above code is using the ES6 Promise syntax, which is the default for pg-promise.
If you prefer the ES7 syntax with async/await, you can use it as well, as shown in the alternative code.
The above code is using the default connection string, which is the only required parameter for the pg-promise initialization.
The default connection string is:
postgres://postgres:postgres@localhost:5432/postgres
It is used when no connection string is provided, and it is used for all the examples in this documentation.
If you want to use a different connection string, you can provide it as the first parameter to the pg-promise initialization:
const pgp = require(‘pg-promise’)({
// Initialization Options
});
const db = pgp(cn); // database instance;
The connection string can be provided in two ways:
1. As a string, as shown in the example above.
2. As an object, with the following properties:
host – server name or IP address;
port – server port;
database – database name;
user – user name;
password – user password;
The connection string can also be provided as a URL, as shown in the alternative code above.
The connection string can be provided in any of the above ways, and it can be provided to the pg-promise initialization, or to the database instance.
If you provide it to both, the database instance will take precedence.
The connection string can also be provided as a connection object, which is the same as the connection string object, but with the following additional properties:
ssl – true/false;
application_name – string;
fallback_application_name – string;
poolSize – integer;
client_encoding – string;
binary – true/false;
The connection object can be provided to the pg-promise initialization, or to the database instance.
If you provide it to both, the database instance will take precedence.
The connection object can also be provided as a connection configuration object, which is the same as the connection object, but with the following additional properties:
connect – function(client, dc, isFresh) {…};
disconnect – function(client, dc) {…};
query – function(e) {…};
error – function(err, e) {…};
receive – function(data, result, e) {…};
task – function(e) {…};
transact – function(e) {…};
The connection configuration object can be provided to the pg-promise initialization, or to the database instance.
If you provide it to both, the database instance will take precedence.
The connection configuration object can also be provided as a connection pool object, which is the same as the connection configuration object, but with the following additional properties:
Promise – any Promise library constructor;
noLocking – true/false;
The connection pool object can be provided to the pg-promise initialization, or to the database instance.
If you provide it to both, the database instance will take precedence.
The connection pool object can also be provided as a database instance, which is the same as the connection pool object, but with the following additional properties:
connect – function(client, dc, isFresh) {…};
disconnect – function(client, dc)