Node.js PostgreSQL Connection

Easily connect your PostgreSQL hosting deployments at ScaleGrid to the Node.js driver to optimize your PostgreSQL management in the cloud.

How to Connect PostgreSQL to the Node.js Driver

  1. Install the PostgreSQL extension for Node.js
    npm install pg
  2. Get the following PostgreSQL deployment information from your cluster details page:
    a. Username
    b. Password
    c. Hostname
    d. Port (default 5432)
    e. SSL certificate (if deployment has SSL)
1920
  1. Run the following code to connect to you PostgreSQL deployment after you enter the information from step 2.
const { Client } = require('pg')
const client = new Client({
  user: 'sgpostgres',
  host: 'SG-PostgreNoSSL-14-pgsql-master.devservers.scalegrid.io',
  database: 'postgres',
  password: 'password',
  port: 5432,
})
client.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});
const { Client } = require('pg');
var fs = require('fs');

const client = new Client({
  user: 'sgpostgres',
  host: 'SG-NewPostgreCluster-5-pgsql-master.devservers.scalegrid.io',
  database: 'postgres',
  password: ‘password’',
  port: 6432,
  ssl  : {
    ca : fs.readFileSync('<path to CA cert file>')
  }
})
client.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});
  1. Run the code using:
    node app.js

👍

Your PostgreSQL deployment is now connected to your Node.js Driver!

If you have any questions, please reach out to us at [email protected].