Heroku Database Connection Properties

眉间皱痕 提交于 2019-11-28 04:52:04

Heroku provides this information for connecting from external sources:

https://devcenter.heroku.com/articles/heroku-postgresql#external-connections-ingress

The second error message indicates PostgreSQL is not configured to accept the connection you're trying to make. Given the information Heroku provides, a good guess is that you're not connecting with SSL. Try to enable that on your connection.

Here are instructions for using SSL with Navicat: http://mirror.navicat.com/manual/online_manual/en/navicat/rv_manual/ClientCert.html.

This may be helpful in configuring Intellij to use SSL: https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#connecting-to-a-database-remotely.

I also had the issue with the FATAL: no pg_hba.conf entry for host error message.

I solved the connection issue to my Heroku Postgres database by adding the following to my JDBC string: &ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory.

Example

jdbc:postgresql://host:port/database?user=username&password=secret&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

You will need the SSL option only if SSL is enabled for your Postgres database (which is the default).

Hint

If you want to check your database connection properties, then just run the following command with the Heroku Toolbelt: heroku pg:info --app your-heroko-appname (make sure that you have Postgres insalled to run this command in your terminal)

The pg:info command will also tell you that sslmode is set to require.

To test the database connection I recommend SQL Power Architect as it is the tool which I was using to check my solution.

IntelliJ -> Datasources and Drivers

After you've configured the host, database and user details under the General tab switch to the Advanced tab and ensure that you've added the following:

  • ssl = true
  • sslfactory = org.postgresql.ssl.NonValidatingFactory
  • sslmode = require

Wanted to help others who might run into this. If you're supplying the Username and Password in seperate fields rather than on the command line, you need to use a ? between the database name and ssl=true and discard the first &

jdbc:postgresql://host:port/database?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

That's the command line that finally allowed me to connect to a PostgreSQL database using SQL Power Architect

For those who might be using Spring Boot and having the configuration provided through the DATABASE_URL environment property (not system property), the suffix can be added to the property:

?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

and passed through with a slight modification to the config bean:

@Bean
public BasicDataSource dataSource() throws URISyntaxException {

    URI dbUri = new URI(System.getenv("DATABASE_URL"));
    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];

    StringBuilder dbUrl = new StringBuilder(128);
    dbUrl.append("jdbc:postgresql://")
            .append(dbUri.getHost()).append(":")
            .append(dbUri.getPort())
            .append(dbUri.getPath());

    String query = dbUri.getQuery();
    if (null != query && !query.isEmpty()) {
        dbUrl.append("?").append(query);
    }

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(dbUrl.toString());
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);

    return basicDataSource;
}

You may consider setting a ENVIRONMENT CONFIG VARIABLE 'PGSSLMODE' to 'require' via Heroku's web interface or CLI.

Case: Postgres dB set up as a Heroku add-on and attached to app on a Heroku Dyno.

Heroku's instructions unfortunately leave out any mention of how to activate SSL, even though it is required for any dB tier starting with Standard-0 by default.

Follow all of the pg-copy or pg-upgrade steps (preferred approach depends on your version of Postgres) in Heroku instructions; however, before decommissioning the old database (if relevant) -- add the PGSSLMODE environment variable.

The instructions sufficiently cover how to promote the new database (and, consequently set the DATABASE_URL), so no changes/modifications to them should be required.

Mayuresh Srivastava

Add or edit the following line in your postgresql.conf file :

listen_addresses = '*'

Add the following line as the first line of pg_hba.conf. It allows access to all databases for all users with an encrypted password:

TYPE DATABASE USER CIDR-ADDRESS  METHOD

host  all  all 0.0.0.0/0 md5

Restart postgresql service:

net stop postgresql-9.0 & net start postgresql-9.0 

(version should be based on your installation) -- On windows (run cmd as an administrator).

sudo service start postgresql -- On linux (or according to your linux distribution.) 

I'm using node.js and was trying to run my knex migrations for my Heroku app. I tried appending ?sslmode=require to the connection URL but it didn't work. I added ?ssl=true instead and now it works perfectly.

Here's an example Heroku PostgreSQL connection URL that works:

postgres://user:password@ec2-12-34-56-78.compute-99.amazonaws.com:port/databasename?ssl=true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!