简体   繁体   中英

Can't connect existing postgres database on heroku via rails

So basically, it is working on my localhost, but when I do the following command line in terminal.

heroku run rake db:migrate 

I get the following error message.

PG::ConnectionBad: could not connect to server: Connection timed out
Is the server running on host "XX.XX.XX.XX" and accepting
TCP/IP connections on port XXXX?

Here's the index method I have on one of my controller in my rails

def index
  sql = "Select * from api.customer"
  @examples = ActiveRecord::Base.connection.execute(sql)
  render json: @examples
end

database.yml file development: adapter: postgresql encoding: utf8 url: <%= ENV['DATABASE_URL'] %> pool: 10

production: adapter: postgresql encoding: utf8 url: <%= ENV['DATABASE_URL'] %> pool: 10

I have included the database info(host, port, database name, username, password) in my database.yml file. Everything in terms of the database info is correct since I multi multi checked before asking. My host is in the format of ip address. I also checked the database with pgadmin 3 and the server configuration(pg_hba.conf and postgresql.conf) is open to all ip addresses. (0.0.0.0/0 md5 and listens to '*') Please advise on what to do.

Thanks!

In Rails the DATABASE_URL ENV var has precedence over any value entered in database.yml .

Heroku sets the Rails database by the DATABASE_URL ENV var (Heroku uses the term config vars which is the exact same thing). It does this automatically when the buildpack detects that you are deploying a rails app after it attaches the postgres addon.

To connect to another DB you need to change DATABASE_URL :

heroku config:set DATABASE_URL=postgresql://xxx.xxx.xxx/myapp

See:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM