pg.connect not a function?
Asked Answered
C

4

21

There appears to be a lot of documentation (e.g. https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js, but also elsewhere including this site) indicating that the proper method of connecting with the pg.js Node package is using pg.connect. However, I attempted (after previous problems with my actual code) to test by using the exact code shown on the aforementioned Heroku documentation:

var pg = require('pg');

pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
  if (err) throw err;
  console.log('Connected to postgres! Getting schemas...');

  client
    .query('SELECT table_schema,table_name FROM information_schema.tables;')
    .on('row', function(row) {
      console.log(JSON.stringify(row));
    });
});

And I got the error message "pg.connect is not a function". What is going on, and how do I fix it?

Cavil answered 18/7, 2017 at 18:6 Comment(0)
E
36

A new version of pg, namely 7.0.0, was published about 15 hours ago (from the time I'm writing this).

This version has lots of changes, one of them being that pg.connect has been hard-deprecated (in other words: removed) in favor of pg.Pool(...).connect(...), as documented here: https://node-postgres.com/guides/upgrading

The new method of connecting looks like this:

var pool = new pg.Pool()

// connection using created pool
pool.connect(function(err, client, done) {
  client.query(/* etc, etc */)
  done()
})

// pool shutdown
pool.end()

Lots of older documentation will not reflect these changes, so the example code they use won't work anymore.

You can either try and rewrite the example code so it works in 7.0.0, or explicitly install an older version that will still work with the example code:

npm install pg@6
Elementary answered 18/7, 2017 at 18:19 Comment(0)
B
12

pg: postgresql => (https://www.npmjs.com/package/pg)

⚠️ pg.connect is deprecated since version 6.3 ❌

BE CAREFUL : Instead there is another method called pool

Here is how you can set up node-postgres easily with express.

const pg        = require('pg');
const express   = require('express');
const app       = express();

const config = {
    user: 'postgres',
    database: 'YOURDBNAME',
    password: 'YOURPASSWORD',
    port: 5432                  //Default port, change it if needed
};

// pool takes the object above -config- as parameter
const pool = new pg.Pool(config);

app.get('/', (req, res, next) => {
   pool.connect(function (err, client, done) {
       if (err) {
           console.log("Can not connect to the DB" + err);
       }
       client.query('SELECT * FROM GetAllStudent()', function (err, result) {
            done();
            if (err) {
                console.log(err);
                res.status(400).send(err);
            }
            res.status(200).send(result.rows);
       })
   })
});

app.listen(4000, function () {
    console.log('Server is running on port 4000');
});
Brusque answered 15/11, 2017 at 13:18 Comment(0)
O
1

If you want to stick with the code you have you can use an older version of Postgres.

First, apply:

npm uninstall postgresql

and then install version 6.1.2 (which is compatible with the code you mentioned):

npm install [email protected]
Ophthalmoscope answered 17/9, 2018 at 17:43 Comment(0)
T
0
var express = require('express');
var app = express();
const pgp = require('pg-promise')();
var connectionString = "";
var parse = require('pg-connection-string').parse;

try {
    var connectionString = "postgres://USERNAME:@localhost:5432/DBNAME";
    var config = parse(connectionString);
    config.password = "PASSWORD";
    var dbcon = pgp(config);
    app.set('dbCon', dbcon);
} 
catch (error) {
    console.log("DB error")
} 

module.exports = app;

Travail answered 17/7, 2018 at 4:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.