最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Connecting Postgres Heroku with Knex not working - Stack Overflow

programmeradmin1浏览0评论

We're having difficulty setting up a Heroku/Postgres database with Knex running our queries. We've set up our heroku/postgres db and created our tables, but we're having difficulty connecting to it with Knex. To make matters worse, there's almost no documentation on connecting Heroku/Postgres with Knex or any ORM so trying to figure this stuff out has been a real pain.

These are the connection patterns that I've tried.

var knex = require('knex')({
  client: 'pg',
  connection: {
    user: username,
    password: password,
    host: host,
    port: port,
    database: database,
    ssl: true
  }
  }
});

And... Note the ssl true was toggle and removed all together to no avail.

  var knex = require('knex')({
    client: 'pg',
    connection: HEROKU_POSTGRESQL_COLOR_URL,
    ssl: true
    }
  });

We've also tried this pattern as well:

var pg = require('knex')({
  client: 'pg',
  connection: HEROKU_POSTGRESQL_COLOR_URL
 });

We haven't yet pulled down a copy of our localdb, so every test we run is basically a git mit. We're basically testing an insert query on a GET request to our root (index.html) page. So on any get request to the main page, it should insert something into our waterrates table. If I switch it from insert to select, it returns an object but you can't actually see any of the data in the object.

The inserts we're attempting to use are:

knex.select('*').from('waterrates').then(function(rows){
  return rows;
});

knex('waterrates').insert({name: 'pleeeaseee work'}, {rate: 100}).then(function(rows){
   console.log(rows);
})

knex.select().

We're actually uncertain where the error could be as attempting to connect doesn't yield any errors. It's probably something silly, but we have no idea where/how to troubleshoot this. Any help would be greatly appreciated!

Thanks, B

We're having difficulty setting up a Heroku/Postgres database with Knex running our queries. We've set up our heroku/postgres db and created our tables, but we're having difficulty connecting to it with Knex. To make matters worse, there's almost no documentation on connecting Heroku/Postgres with Knex or any ORM so trying to figure this stuff out has been a real pain.

These are the connection patterns that I've tried.

var knex = require('knex')({
  client: 'pg',
  connection: {
    user: username,
    password: password,
    host: host,
    port: port,
    database: database,
    ssl: true
  }
  }
});

And... Note the ssl true was toggle and removed all together to no avail.

  var knex = require('knex')({
    client: 'pg',
    connection: HEROKU_POSTGRESQL_COLOR_URL,
    ssl: true
    }
  });

We've also tried this pattern as well:

var pg = require('knex')({
  client: 'pg',
  connection: HEROKU_POSTGRESQL_COLOR_URL
 });

We haven't yet pulled down a copy of our localdb, so every test we run is basically a git mit. We're basically testing an insert query on a GET request to our root (index.html) page. So on any get request to the main page, it should insert something into our waterrates table. If I switch it from insert to select, it returns an object but you can't actually see any of the data in the object.

The inserts we're attempting to use are:

knex.select('*').from('waterrates').then(function(rows){
  return rows;
});

knex('waterrates').insert({name: 'pleeeaseee work'}, {rate: 100}).then(function(rows){
   console.log(rows);
})

knex.select().

We're actually uncertain where the error could be as attempting to connect doesn't yield any errors. It's probably something silly, but we have no idea where/how to troubleshoot this. Any help would be greatly appreciated!

Thanks, B

Share Improve this question asked Sep 29, 2014 at 22:03 BMACBMAC 1412 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I had an old version of PG installed which was causing the issue. We changed our package.json file to use the latest PG. Heroku updated it and it worked!

As a side note, if anyone looks at this in the future, heroku requires an SSL connection. Keep that in mind when you're working. The connection string that I provided above should work for you all.

B

For anyone who is dealing with this problem.

Please set "SSL" to false if you don't use paid dynos.

Heroku only assigns "SSL" to users who use paid dynos.

const db = knex({
  client: 'pg',
  connection: {
    connectionString : process.env.DATABASE_URL,
    ssl: false
  }
});
发布评论

评论列表(0)

  1. 暂无评论