Heroku: Pg::Connectionbad: Could Not Connect to Server: Connection Refused

Heroku: PG::ConnectionBad: could not connect to server: Connection refused

In your application.rb file, try setting this:

config.assets.initialize_on_precompile = false

and see if that fixes your issue.

Also, if you already did not pre-provision the database for your app, you need to create one:

heroku addons:create heroku-postgresql

You can verify the database was added to your application by running:

heroku config --app your_app_name

PG::ConnectionBad - could not connect to server: Connection refused Sinatra server on Heroku

You don't need the other lines if you have the db url, it can be a 1 liner, and also let postgres timeout it's default, no need to set it. You probably want a larger pool size in prod. Go to the postgres database you should have added to your app in Heroku. Click on the View Credentials button. Copy the data from there to your local .env file and also add .env to your .gitignore file because you don't want to commit your private credentials to your public repo.

Then on heroku

database.yml:

development:
adapter: sqlite3
database: db/madlibs.sqlite
host: localhost
pool: 5
timeout: 5000

production:
adapter: postgresql
encoding: unicode
host: <%= ENV['DB_HOST'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
port: <%= ENV['DB_PORT'] %>
database: <%= ENV['DB_NAME'] %>
pool: 25

Alternatively, you could just use 1 environment variable which concatenates all of this into a URI which would look something like

#postgresql://username:password@db-shared-us-east-1-or-something.blah:1234/db
#If you use this format you can use just a single environment variable somthinglike

production:
adapter: postgresql
encoding: unicode
url: <%= ENV['DATABASE_URI'] %>
pool: 25

In either case you will need to set whatever environment variables you'll use in your database.yml file set in your Heroku dashboard at: https://dashboard.heroku.com/apps/yourappname/settings and click on Reveal Config Vars. This is where you will set the needed vars as this is where your app will load them from. Set either the vars in my first example, or a single one as in my 2nd example. Either way should work.

Make sure you have the pg gem in your production group in Gemfile. And make sure you've set your environment variables on your heroku server.

PG::ConnectionBad - could not connect to server: Connection refused

It could be as simple as a stale PID file. It could be failing silently because your computer didn't complete the shutdown process completely which means postgres didn't delete the PID (process id) file.

The PID file is used by postgres to make sure only one instance of the server is running at a time. So when it goes to start again, it fails because there is already a PID file which tells postgres that another instance of the server was started (even though it isn't running, it just didn't get to shutdown and delete the PID).

  1. To fix it remove/rename the PID file. Find the postgres data directory. On macOS using homebrew it is in /usr/local/var/postgres/,
    or /usr/local/var/log/ other systems it might be /usr/var/postgres/. On M1, it might be /opt/homebrew/var/postgresql.
  2. To make sure this is the problem, look at the log file (server.log). On the last lines you will see:

FATAL: lock file "postmaster.pid" already exists

HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?


  1. If so, rm postmaster.pid

  2. Restart your server. On a mac using launchctl (with homebrew) the following commands will restart the server.

    brew services restart postgresql

OR on older versions of Brew

    launchctl unload homebrew.mxcl.postgresql.plist  
launchctl load -w homebrew.mxcl.postgresql.plist

PG::ConnectionBad: could not connect to server: Connection refused (Ubuntu 16.04, Rails 5, Capistrano)

As it turns out, I had the wrong ip address in the database.yml file. I had it set to my host ip (138.68.6.26) when it should have been set to the local ip (127.0.0.1).

PG::ConnectionBad: could not connect to server: Connection refused

Adding the following line in config/application.rb was the solution to my problem

 config.assets.initialize_on_precompile = false

rake aborted! PG::ConnectionBad: could not connect to server: Connection refused

This means that Postgresql isn't running so it cannot make the database.

Have you installed Postgres? If so, you can likely start the service by using:

sudo service postgresql restart

If Postgres isn't installed, you first need to install it. How to do so differs depending on your system:

https://www.postgresqltutorial.com/install-postgresql/

Heroku rake aborted (PG::ConnectionBad: could not connect to server: No such file or directory)

Your database won't be on the same server as your code, and you can't create it yourself. Instead, Heroku tells you how to connect via the DATABASE_URL environment variable.

The code you reference appears to load its database information from a file without looking at the environment. You'll have to modify it to use ENV['DATABASE_URL'] when that variable is set.



Related Topics



Leave a reply



Submit