Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to Heroku postgres from outside of Heroku #278

Closed
thiago-soliveira opened this issue Feb 11, 2017 · 25 comments
Closed

Unable to connect to Heroku postgres from outside of Heroku #278

thiago-soliveira opened this issue Feb 11, 2017 · 25 comments
Labels

Comments

@thiago-soliveira
Copy link

In order to connect to Heroku postgres from outside of Heroku we need to use SSL.

So, I'm getting the following message when trying to connect typeorm to the Heroku postgres.

"message": "no pg_hba.conf entry for host "xxx", user "xxx", database "xxx", SSL off"

I believe this is happening because the postgres driver is trying to connect without SSL. If I'm correct, how can I tell the sql driver to use SSL.

@pleerock
Copy link
Member

Solution is here. To do so in typeorm you need to provide option in special "extra" section of connection options:

createConnection(
    driver: {
        type: "postgres",
        host: "localhost",
        port: 5432,
        username: "root",
        password: "admin",
        database: "test",
        extra: {
             ssl: true
        }
    },);

@AngelMunoz
Copy link

hey sorry old issue, but how do you set this with .env file or env vars?

@pleerock
Copy link
Member

You need to set

TYPEORM_DRIVER_EXTRA = {"ssl":true}

@AngelMunoz
Copy link

thanks it did work

@pie6k
Copy link

pie6k commented Mar 11, 2018

In my case:

  1. put what you get from heroku config:get DATABASE_URL to your .env file (heroku will automatically add that to process.env

  2. connect with:

createConnection({
    url: process.env.DATABASE_URL,
    type: 'postgres',
    entities: [YOUR ENTITIES GO HERE],
    synchronize: true,
    extra: {
      ssl: true,
    },
  });

@N-CP
Copy link

N-CP commented Jun 27, 2018

I m trying to connect postgresql heroku to oracle sql developer. But i m getting the below error

message": "no pg_hba.conf entry for host "xxx", user "xxx", database "xxx", SSL off"

what could be the solution?
can anybody help me in this pls??

@jmaicaaan
Copy link

@N-CP, what is your ormconfig looks like?

@a1300
Copy link

a1300 commented Jan 17, 2019

For me it worked with:

createConnection({
    url: process.env.DATABASE_URL,
    type: 'postgres',
    entities: [YOUR ENTITIES GO HERE],
    synchronize: true,
+   ssl: true,
  });

@yaroslav-ilin
Copy link

I believe the original issue and the solutions (both JSON & env) has to be documented.

Would such pull request for docs website be accepted?

@Kononnable
Copy link
Contributor

SSL options are already documented (https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md). However if you find a good place to remind about that(common problem?) PR will be accepted.

@AllanPamplona-zz
Copy link

Also PGSSLMODE=require solves this problem.

@rakeshta
Copy link

For me it worked with:

createConnection({
    url: process.env.DATABASE_URL,
    type: 'postgres',
    entities: [YOUR ENTITIES GO HERE],
    synchronize: true,
+   ssl: true,
  });

worked for me too.

@idudinov
Copy link

idudinov commented Nov 26, 2019

hi there!
still don't able to connect to remote Heroku Postgres server from local machine, my config looks like:

const config: PostgresConnectionOptions = {
    ...baseConfig, // here're common settings
    url: process.env.DATABASE_URL, // got from DATABASE_URL config var in Heroku
    ssl: true, // double check
    extra: {
        ssl: true,
    },
};

also tried to set up PGSSLMODE=require as env var.

TypeORM in package.json:
"typeorm": "0.2.20"

getting the error:

Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
    at TLSSocket.emit (events.js:210:5)
    at TLSSocket._finishInit (_tls_wrap.js:794:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12) {
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}

not sure what certificate should I use, or can I bypass it somehow?

thanks in advance!

@idudinov
Copy link

idudinov commented Nov 26, 2019

jeez it fails on Heroku as well!

UPDATE: sorry, false alarm! it's actually node-postgres issue: brianc/node-postgres#2009

@JakeSidSmith
Copy link

@idudinov

getting the error:

Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
    at TLSSocket.emit (events.js:210:5)
    at TLSSocket._finishInit (_tls_wrap.js:794:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12) {
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}

not sure what certificate should I use, or can I bypass it somehow?

thanks in advance!

I've just run into this issue and it seems that node-postgres have added an option to disable rejecting unauthorized connections. I don't think this is safe to run in production though. I'm just using it to run my migrations.

I believe the ideal solution is to get a new SSL certificate, but for now I have:

  ssl: true,
  extra: {
    ssl: {
      rejectUnauthorized: false,
    },
  },

@aliskhanoff
Copy link

@idudinov

getting the error:

Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
    at TLSSocket.emit (events.js:210:5)
    at TLSSocket._finishInit (_tls_wrap.js:794:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12) {
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}

not sure what certificate should I use, or can I bypass it somehow?
thanks in advance!

I've just run into this issue and it seems that node-postgres have added an option to disable rejecting unauthorized connections. I don't think this is safe to run in production though. I'm just using it to run my migrations.

I believe the ideal solution is to get a new SSL certificate, but for now I have:

  ssl: true,
  extra: {
    ssl: {
      rejectUnauthorized: false,
    },
  },

it works for me. Thanks!

@italodeandra
Copy link

What fixed for me while using Postgres from Heroku was only adding the following environment variable:

PGSSLMODE=no-verify

@Haple
Copy link

Haple commented Aug 21, 2020

What worked for me:

{
...
    "password": ...,
    "database": ...,
    "ssl": {
        "rejectUnauthorized": false,
    },
...
}

@kvarela
Copy link

kvarela commented Oct 31, 2020

What worked for me:

{
...
    "password": ...,
    "database": ...,
    "ssl": {
        "rejectUnauthorized": false,
    },
...
}

Yes, this for me also, AND I had to REMOVE:

{
  extra: {
    ssl: true
  }
}

What a PITA

dansteren added a commit to ardentink/adr-api that referenced this issue Dec 19, 2020
When running migrations on DOAP I got the following error:

Error during migration run:
error: no pg_hba.conf entry for host "134.209.211.49", user "db",
database "db", SSL off.

This should fix it.

See typeorm/typeorm#278
@altschuler
Copy link

If, like me, you added ?ssl=true to the url, then the rejectUnauthorized wont work. You need to either remove the ssl param from the URL or set it to ssl=no-verify in which case you can remove the ssl options from the typeorm config altogether. Found the answer here brianc/node-postgres#2009 (comment)

@agilatakishiyev
Copy link

@idudinov

getting the error:

Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
    at TLSSocket.emit (events.js:210:5)
    at TLSSocket._finishInit (_tls_wrap.js:794:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12) {
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}

not sure what certificate should I use, or can I bypass it somehow?
thanks in advance!

I've just run into this issue and it seems that node-postgres have added an option to disable rejecting unauthorized connections. I don't think this is safe to run in production though. I'm just using it to run my migrations.

I believe the ideal solution is to get a new SSL certificate, but for now I have:

  ssl: true,
  extra: {
    ssl: {
      rejectUnauthorized: false,
    },
  },

thanks a lot it worked for me too
i was using typeorm and nest.js and i was getting this error while connecting a database located in digitalocean

@benjaminudoh10
Copy link
Contributor

@idudinov

getting the error:

Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
    at TLSSocket.emit (events.js:210:5)
    at TLSSocket._finishInit (_tls_wrap.js:794:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12) {
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}

not sure what certificate should I use, or can I bypass it somehow?
thanks in advance!

I've just run into this issue and it seems that node-postgres have added an option to disable rejecting unauthorized connections. I don't think this is safe to run in production though. I'm just using it to run my migrations.

I believe the ideal solution is to get a new SSL certificate, but for now I have:

  ssl: true,
  extra: {
    ssl: {
      rejectUnauthorized: false,
    },
  },

Thank you so much for this. I have searched everywhere. All the solutions on this thread did not work for me except this.

Thanks again.

@JakeSidSmith
Copy link

@idudinov

getting the error:

Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
    at TLSSocket.emit (events.js:210:5)
    at TLSSocket._finishInit (_tls_wrap.js:794:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12) {
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}

not sure what certificate should I use, or can I bypass it somehow?
thanks in advance!

I've just run into this issue and it seems that node-postgres have added an option to disable rejecting unauthorized connections. I don't think this is safe to run in production though. I'm just using it to run my migrations.

I believe the ideal solution is to get a new SSL certificate, but for now I have:

  ssl: true,
  extra: {
    ssl: {
      rejectUnauthorized: false,
    },
  },

Just as a follow up, since Heroku enforced SSL for all postgres connections the above is the solution they recommend.

So I guess it is safe for production. 😊

@revskill10
Copy link

Guys, the issue is in the PGSSLMODE=no-verify combined with

extra: {
    ssl: {
      rejectUnauthorized: false,
    },
  },

@lenybernard
Copy link

I resolve my issue by setting NODE_TLS_REJECT_UNAUTHORIZED=0 to heroku env vars (see https://stackoverflow.com/a/45088585).

This was not a typeorm issue, it's because heroku use self signed certificates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests