Rectangle 27 9

Nginx is a reverse proxy server - its role on your server is to accept HTTP requests and proxy them to another process on the same host. The "upstream" that the error message is talking about is referring to the bit in nginx's configuration (part of which is the /etc/nginx/sites-available/default file) that tells it where to send incoming requests. The error message you're seeing indicates that nginx received a request, but couldn't send it to the other process it was supposed to.

When your server rebooted, the nginx process started back up, but your Rails process -- the one that's meant to be listening on port 3001 -- did not!

How you restart the Rails process depends on the way that you started it before and the way your server is configured. It may be as simple as cd'ing into your Rails application's directory on the server and running:

rails server -b 127.0.0.1 -p 3001 -e production -d

...but, to prevent problems like this from happening in the future (and to improve the performance of your Rails app!), it would be better to use some kind of production-ready Rails application server. I would recommend using Phusion Passenger because it's the most turn-key solution -- their user's guide for nginx describes installation and configuration -- but there are plenty of alternatives. There's a great writeup of what your options are, what they all mean, and how they relate on the top answer of this StackOverflow question.

Thans Ash! I'm using Thin as my app server. How can I ensure that Thin is running? How do I debug the configuration between nginx and Thin?

Ah, good, you're using something other than WEBrick already! I haven't used Thin specifically myself, but a quick ps -ef | grep thin should show you if any thin processes are running.

Also: it looks like sudo thin install will actually write you an /etc/init.d script that will make sure thin runs on boot. That's handy! Here's a blog post I found that talks about it a little: jordanhollinger.com/2011/04/22/how-to-use-thin-effectivly

ruby on rails - connect() failed (111: Connection refused) while conne...

ruby-on-rails ruby-on-rails-4 nginx rackspace-cloud rackspace
Rectangle 27 9

Nginx is a reverse proxy server - its role on your server is to accept HTTP requests and proxy them to another process on the same host. The "upstream" that the error message is talking about is referring to the bit in nginx's configuration (part of which is the /etc/nginx/sites-available/default file) that tells it where to send incoming requests. The error message you're seeing indicates that nginx received a request, but couldn't send it to the other process it was supposed to.

When your server rebooted, the nginx process started back up, but your Rails process -- the one that's meant to be listening on port 3001 -- did not!

How you restart the Rails process depends on the way that you started it before and the way your server is configured. It may be as simple as cd'ing into your Rails application's directory on the server and running:

rails server -b 127.0.0.1 -p 3001 -e production -d

...but, to prevent problems like this from happening in the future (and to improve the performance of your Rails app!), it would be better to use some kind of production-ready Rails application server. I would recommend using Phusion Passenger because it's the most turn-key solution -- their user's guide for nginx describes installation and configuration -- but there are plenty of alternatives. There's a great writeup of what your options are, what they all mean, and how they relate on the top answer of this StackOverflow question.

Thans Ash! I'm using Thin as my app server. How can I ensure that Thin is running? How do I debug the configuration between nginx and Thin?

Ah, good, you're using something other than WEBrick already! I haven't used Thin specifically myself, but a quick ps -ef | grep thin should show you if any thin processes are running.

Also: it looks like sudo thin install will actually write you an /etc/init.d script that will make sure thin runs on boot. That's handy! Here's a blog post I found that talks about it a little: jordanhollinger.com/2011/04/22/how-to-use-thin-effectivly

ruby on rails - connect() failed (111: Connection refused) while conne...

ruby-on-rails ruby-on-rails-4 nginx rackspace-cloud rackspace
Rectangle 27 459

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).

  • To fix it remove/rename the PID file. Find the postgres data directory. On a MAC using homebrew it is /usr/local/var/postgres/, other systems it might be /usr/var/postgres/.
server.log
FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?
rm postmaster.pid

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

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

OR on newer versions of Brew

FATAL:  could not open directory "pg_tblspc": No such file or directory
gem uninstall pg
brew uninstall postgres
rm -rf /usr/local/var/postgres
brew install postgres
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
ARCHFLAGS="-arch x86_64" gem install pg

is the postmaster.opts file the same as the postmaster.pid file?

postmaster.pid
brew services restart postgresql

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 454

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).

  • To fix it remove/rename the PID file. Find the postgres data directory. On a MAC using homebrew it is /usr/local/var/postgres/, other systems it might be /usr/var/postgres/.
server.log
FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?
rm postmaster.pid

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

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

OR on newer versions of Brew

FATAL:  could not open directory "pg_tblspc": No such file or directory
gem uninstall pg
brew uninstall postgres
rm -rf /usr/local/var/postgres
brew install postgres
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
ARCHFLAGS="-arch x86_64" gem install pg

is the postmaster.opts file the same as the postmaster.pid file?

postmaster.pid
brew services restart postgresql

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 10

I have renamed mobile.socket to mobile.sock in the relevant config files (unicorn.rb and nginx default) and all is good, no need to create any socket files, it just works as expected.

This also happens if the app server is not running (In my case unicorn). Unicorn creates the socket and nginx looks for it. If the socket is not there nginx kicks up a fuss, so if you are reading this looking for a solution make sure your app server (unicorn) is running and make sure all your socket names match in the various configuration files (unicorn.rb and whatever nginx.conf file has the socket mentioned in it)

Thanks for this. I didn't realize my unicorn was not starting properly in my init script.

ruby on rails - nginx and unicorn bad gatewayconnect() to unix:/tmp/mo...

ruby-on-rails nginx unicorn rackspace-cloud rackspace
Rectangle 27 32

I have managed to solve the problem by following Chris Slade's answer, but to restart the server I had to use the following commands

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 31

I have managed to solve the problem by following Chris Slade's answer, but to restart the server I had to use the following commands

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 28

After a lot of searching and analysis, I found a solution if you are using ubuntu just write this command in your terminal and hit enter

sudo service postgresql restart

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 26

After a lot of searching and analysis, I found a solution if you are using ubuntu just write this command in your terminal and hit enter

sudo service postgresql restart

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 22

To fix these type of issues with Postgres and to work with Postgres on Mac OSX, this is probably the BEST and the EASIEST solution that I have found so far:

Just download, install and be happy :)

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 21

To fix these type of issues with Postgres and to work with Postgres on Mac OSX, this is probably the BEST and the EASIEST solution that I have found so far:

Just download, install and be happy :)

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 20

Do you have postgresql installed within your system? If not, then watch Install postgresql. After you successfully integrate postgresql into your system you can type something like that in your system terminal:

which psql
#=> /usr/bin/psql

After that you need to create a user and database in postgresql like this:

sudo su - postgres
psql

Then you can see the following within your terminal

postgres=#
CREATE USER yourname WITH PASSWORD 'passwordhere';
CREATE DATABASE metals-directory_production  WITH OWNER yourname;
GRANT ALL PRIVILEGES ON DATABASE metals-directory_production TO yourname;
development:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_development
  pool: 5
  username: yourname
  password: passwordhere   ### password you have specified within psql
  host: localhost
  port: 5432               ### you can configure it in file postgresql.conf

Also if you have problems with postgresql it is good idea to check pg_hba.conf

I have it installed but the location is at /usr/local/bin/psql and when I run sudo -u postgres it says sudo: unknown user: postgres

psql should work if you want to use pg in your app. Try type in terminal psql metals-directory_development. Any results? If not, then try to reinstall postgres try this for example

When I run psql metals-directory_development I get psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? I'm a beginner rails programmer and I have no idea what that means. I've tried reinstalling postgres numerous times but no luck.

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 20

Do you have postgresql installed within your system? If not, then watch Install postgresql. After you successfully integrate postgresql into your system you can type something like that in your system terminal:

which psql
#=> /usr/bin/psql

After that you need to create a user and database in postgresql like this:

sudo su - postgres
psql

Then you can see the following within your terminal

postgres=#
CREATE USER yourname WITH PASSWORD 'passwordhere';
CREATE DATABASE metals-directory_production  WITH OWNER yourname;
GRANT ALL PRIVILEGES ON DATABASE metals-directory_production TO yourname;
development:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_development
  pool: 5
  username: yourname
  password: passwordhere   ### password you have specified within psql
  host: localhost
  port: 5432               ### you can configure it in file postgresql.conf

Also if you have problems with postgresql it is good idea to check pg_hba.conf

I have it installed but the location is at /usr/local/bin/psql and when I run sudo -u postgres it says sudo: unknown user: postgres

psql should work if you want to use pg in your app. Try type in terminal psql metals-directory_development. Any results? If not, then try to reinstall postgres try this for example

When I run psql metals-directory_development I get psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? I'm a beginner rails programmer and I have no idea what that means. I've tried reinstalling postgres numerous times but no luck.

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 10

gem uninstall pg
brew uninstall postgres

Nuke the postgres folder which might be lingering with a bunch of stale stuff it in...

rm -rf /usr/local/var/postgres
brew install postgres
brew install services

...and start pg with it...

brew services start postgresql
gem install pg

Be careful with this method... this is a scorched earth approach and will most likely destroy any local dbs you currently have. Have a look at github.com/copiousfreetime/launchy where you might be able to do lunchy stop postgres followed by lunchy start postgres

Heavy handed fix but worked for me!

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 10

gem uninstall pg
brew uninstall postgres

Nuke the postgres folder which might be lingering with a bunch of stale stuff it in...

rm -rf /usr/local/var/postgres
brew install postgres
brew install services

...and start pg with it...

brew services start postgresql
gem install pg

Be careful with this method... this is a scorched earth approach and will most likely destroy any local dbs you currently have. Have a look at github.com/copiousfreetime/launchy where you might be able to do lunchy stop postgres followed by lunchy start postgres

Heavy handed fix but worked for me!

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 9

check the file postgresql.conf (on ubuntu is in /etc/postgresql/X.X/main/postgresql.conf ) and look for the line that says

listen_addresses="localhost"
listen_addresses="*"

it would be accepting every IP's, next check the line that says

port=5432

and check if is the same port of your database.yml, by default on my postgresql-9.2 use 5433 instead 5432, don't forget to restart the postgres server, good luck

Thanks - this helped me. The port was the problem for me.

This was my problem as well. I suspect it happened because I already had 9.3 running when I tried to "upgrade" (i.e. install 9.5). I'm guessing it was trying to be polite and configure itself to run in parallel on a different port rather than take over the existing install/process.

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 9

check the file postgresql.conf (on ubuntu is in /etc/postgresql/X.X/main/postgresql.conf ) and look for the line that says

listen_addresses="localhost"
listen_addresses="*"

it would be accepting every IP's, next check the line that says

port=5432

and check if is the same port of your database.yml, by default on my postgresql-9.2 use 5433 instead 5432, don't forget to restart the postgres server, good luck

Thanks - this helped me. The port was the problem for me.

This was my problem as well. I suspect it happened because I already had 9.3 running when I tried to "upgrade" (i.e. install 9.5). I'm guessing it was trying to be polite and configure itself to run in parallel on a different port rather than take over the existing install/process.

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 6

PG::ConnectionBad - could not connect to server: Connection refused
9.5 -> 9.6
9 -> 10

I got this error after having run brew upgrade postgresql after the release of PostgreSQL version 9.6. The problem is that major/minor version upgrades require additional steps to migrate old date to the new version.

You can check if this is the problem by checking the the latest brew formula PostgreSQL version installed with homebrew...

$ brew info postgresql

/usr/local/Cellar/postgresql/9.5.4_1 (3,147 files, 35M)
Poured from bottle on 2016-10-14 at 13:33:28
/usr/local/Cellar/postgresql/9.6.1 (3,242 files, 36.4M) *
Poured from bottle on 2017-02-06 at 12:41:00
$ cat /usr/local/var/postgres/PG_VERSION
9.5

If the PG_VERSION is less than the latest brew formula and the difference is a major/minor version change, then this is probably your problem.

Step 1. Make sure PostgreSQL is switched off:

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql

Step 2. Make a new pristine database:

$ initdb /usr/local/var/postgres9.6 -E utf8

Step 3. Check what the old and new binary versions are:

$ ls /usr/local/Cellar/postgresql/
9.5.3   9.5.4   9.6.1

Note that in this example I am upgrading from 9.5.4 binary to 9.6.1 binary

Step 4. Migrate the current data to the new database using the pg_upgrade utility.

$ pg_upgrade \
  -d /usr/local/var/postgres \
  -D /usr/local/var/postgres9.6 \
  -b /usr/local/Cellar/postgresql/9.5.4/bin/ \
  -B /usr/local/Cellar/postgresql/9.6.1/bin/ \
  -v
  • -d flag specifies the current data directory
-D
  • -b specifies the old binary
-B

Step 5. Move the old data directory out of the way

$ mv /usr/local/var/postgres /usr/local/var/postgres9.5
$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, if you're running a current version of Homebrew
$ brew services start postgresql

Step 8. If youre using the pg gem for Rails, you should recompile by uninstalling and reinstalling the gem (skip this step if you're not using the pg gem)

$ gem uninstall pg
$ gem install pg

Step 9.(optional) After you've reassured yourself that everything is working OK, you can run regain some disk space with the following command:

brew cleanup postgresql

...and if you're feeling really brave you can delete the old PostgreSQL data directory with the following command

rm -rf /usr/local/var/postgres9.5/

This was exactly the problem I experienced. This solution worked perfectly. Thank you.

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 6

PG::ConnectionBad - could not connect to server: Connection refused
9.5 -> 9.6
9 -> 10

I got this error after having run brew upgrade postgresql after the release of PostgreSQL version 9.6. The problem is that major/minor version upgrades require additional steps to migrate old date to the new version.

You can check if this is the problem by checking the the latest brew formula PostgreSQL version installed with homebrew...

$ brew info postgresql

/usr/local/Cellar/postgresql/9.5.4_1 (3,147 files, 35M)
Poured from bottle on 2016-10-14 at 13:33:28
/usr/local/Cellar/postgresql/9.6.1 (3,242 files, 36.4M) *
Poured from bottle on 2017-02-06 at 12:41:00
$ cat /usr/local/var/postgres/PG_VERSION
9.5

If the PG_VERSION is less than the latest brew formula and the difference is a major/minor version change, then this is probably your problem.

Step 1. Make sure PostgreSQL is switched off:

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql

Step 2. Make a new pristine database:

$ initdb /usr/local/var/postgres9.6 -E utf8

Step 3. Check what the old and new binary versions are:

$ ls /usr/local/Cellar/postgresql/
9.5.3   9.5.4   9.6.1

Note that in this example I am upgrading from 9.5.4 binary to 9.6.1 binary

Step 4. Migrate the current data to the new database using the pg_upgrade utility.

$ pg_upgrade \
  -d /usr/local/var/postgres \
  -D /usr/local/var/postgres9.6 \
  -b /usr/local/Cellar/postgresql/9.5.4/bin/ \
  -B /usr/local/Cellar/postgresql/9.6.1/bin/ \
  -v
  • -d flag specifies the current data directory
-D
  • -b specifies the old binary
-B

Step 5. Move the old data directory out of the way

$ mv /usr/local/var/postgres /usr/local/var/postgres9.5
$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, if you're running a current version of Homebrew
$ brew services start postgresql

Step 8. If youre using the pg gem for Rails, you should recompile by uninstalling and reinstalling the gem (skip this step if you're not using the pg gem)

$ gem uninstall pg
$ gem install pg

Step 9.(optional) After you've reassured yourself that everything is working OK, you can run regain some disk space with the following command:

brew cleanup postgresql

...and if you're feeling really brave you can delete the old PostgreSQL data directory with the following command

rm -rf /usr/local/var/postgres9.5/

This was exactly the problem I experienced. This solution worked perfectly. Thank you.

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg
Rectangle 27 6

As suggested above, I just opened up the Postgres App on my Mac, clicked Open Psql, closed the psql window, restarted my rails server in my terminal, and it was working again, no more error.

ruby on rails - PG::ConnectionBad - could not connect to server: Conne...

ruby-on-rails ruby database-connection rails-postgresql pg