Skip to content

Examples PostgreSQL Connection

Michael Anderson edited this page Dec 12, 2017 · 1 revision

The PostgreSQL driver connects to a local or remote PostgreSQL server over an IP connection. The PostgreSQL driver supports transactions.

Connection URL

postgres://[[username]:[password]@]host[:port]/database

Usage

const Connection = require('database-js2').Connection;

var connection = new Connection('postgres://user:password@localhost/data');

Select Some Data

var Connection = require('database-js2').Connection;

(async function() {
    let connection, statement, rows;
    connection = new Connection('postgres://my_secret_username:my_secret_password@localhost:5432/my_top_secret_database');

    try {
        statement = await connection.prepareStatement("SELECT * FROM tablea WHERE user_name = ?");
        rows = await statement.query('not_so_secret_user');
        console.log(rows);
    } catch (error) {
        console.log(error);
    } finally {
        await connection.close();
    }
})();