Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres won't connect to server in Ruby on Rails app using c9.io

A few days ago, I was able to successfully install Postgresql and create/migrate my databases over from SQLite (in preparation for deploying my Rails 4 app)...or so I thought.

I restarted my server, but when I try to access my app, I get this error:

PG::ConnectionBad
could not connect to server: Connection refused 
Is the server running locally and accepting connections on Unix domain socket 
"/var/run/postgresql/.s.PGSQL.5432"?

I've seen several similar answered questions on SO, but they all involve Macs. Since i'm using the Cloud 9 IDE instead, I thought there might be differences (for instance, the mentions of localhost are probably not relevant, because that's not how I access the app currently on c9.io.)

According to the answers on the other questions, I tried this in my terminal:

sudo ps auxw | grep post

which gave me:

ubuntu    2527  0.0  0.0  10552   888 pts/5    S+   15:21   0:00 grep --color=auto post

then I tried:

sudo find / -name .s.PGSQL.5432 -ls

and I got:

5286    0 lrwxrwxrwx   1 root     root           26 May 11 15:13 /var/pgsql_socket/.s.PGSQL.5432 -> /private/tmp/.s.PGSQL.5432
5183    0 srwxrwxrwx   1 postgres postgres        0 May 10 22:18 /run/postgresql/.s.PGSQL.5432

yamilethmedina@wheels_registration:~/workspace (master|MERGING) $ psql
psql: could not connect to server: Connection refused
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Here is the full trace of my server:

Started GET "/" for 12.43.117.2 at 2015-05-11 16:35:48 +0000

PG::ConnectionBad (could not connect to server: Connection refused
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
):
activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `initialize'
activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `new'
activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect'
activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize'
activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in  `postgresql_connection'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout'
/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout'
activerecord (4.1.6)  lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
activerecord (4.1.6) lib/active_record/connection_handling.rb:113:in `retrieve_connection'
activerecord (4.1.6) lib/active_record/connection_handling.rb:87:in `connection'
activerecord (4.1.6) lib/active_record/migration.rb:388:in `check_pending!'
activerecord (4.1.6) lib/active_record/migration.rb:377:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.6)   lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.6) lib/rails/engine.rb:514:in `call'
railties (4.1.6) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'


Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms)
Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms)
Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.9ms)
Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (45.7ms)

My entire github repo just in case: https://github.com/yamilethmedina/wheels_registration.git

like image 532
Yami Medina Avatar asked May 11 '15 16:05

Yami Medina


People also ask

How do I connect to PostgreSQL from Ruby on rails?

Create a PostgreSQL user so that your Ruby on Rails application will be able to connect to the PostgreSQL database: This should be the same username which you used to install and run Ruby on Rails.

How do I set up a rails app with Postgres?

To do this, switch into the Postgres user: Then create a user (or a "role", as Postgres calls it): To create a Rails app configured for Postgres, run this command: This creates a directory called "myapp" which houses an app called "myapp" (you can name it anything you like when running the command).

How do I delete a Rails application with PostgreSQL?

Delete Rails application If you have used the default database which is SQLite, you can delete the Rails application by simply deleting its app directory. If you've used PostgreSQL, first you need to drop the related databases of your application using command: Now you know how to create a Rails application with PostgreSQL as the database.

Can I use Postgres instead of MySQL in rails?

By default it assumes that MySQL is being used, but it’s quite easy to use with Postgres instead. This guide will step you through creating a Rails application that uses a Postgres database.


1 Answers

It doesn't look like postgresql is running. Perhaps it is not set to auto start. One reason for this can be if postgresql wasn't cleanly shutdown on the last run, and leaves a pid file laying around.

Try

  sudo service postgresql start 

and see if it comes back, and just make sure that you shut the vm down (so the stop scripts can run) as opposed to just powering it off.

like image 54
Doon Avatar answered Sep 27 '22 17:09

Doon