Can't deploy Rails App, push fails (PG::ConnectionBad: could not connect to server: Connection refused)

November 4, 2014 8k views

Hi, after playing with DigitalOcean for a while I am setting up an instance for my customer and I am unable to push the app, because of database error.

PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
       TCP/IP connections on port 5432?

Which seems weird, since I bet the port is different on DO, but I don't see how this can be if dokku/docker replaces the database.yml?

My database.yml is almost the same as on my other app which runs just fine.

development:
  database: cttv
  adapter: postgresql
  user: michal  
  host: localhost

test: &test
  database: cttv_test
  adapter: postgresql
  user: michal  
  host: localhost

production:
  database: cttv
  adapter: postgresql
  user: michal  
  host: localhost

cucumber:
  <<: *test

I think I did all the steps, but let me know if something is missing:

  1. installed postgres
  2. created database
  3. linked my app
  4. added rails_12factor to gemfile
  5. relinked the app with db to be sure with a script

Listing the processes reveal postgres should be running:

root@catchtalk:/var/lib/dokku/plugins# ps -ax | grep postg
 4591 ?        S      0:00 /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf
 4593 ?        Ss     0:00 postgres: checkpointer process                                                                                              
 4594 ?        Ss     0:00 postgres: writer process                                                                                                    
 4595 ?        Ss     0:00 postgres: wal writer process                                                                                                
 4596 ?        Ss     0:00 postgres: autovacuum launcher process                                                                                       
 4597 ?        Ss     0:00 postgres: stats collector process                                                                                           
 5990 ?        S      0:00 su postgres sh -c /usr/lib/postgresql/9.3/bin/postgres           -D  /var/lib/postgresql/9.3/main  -c config_file=/etc/postgresql/9.3/main/postgresql.conf  -c listen_addresses=*
 5991 ?        Ss     0:00 /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf -c listen_addresses=*
 5993 ?        Ss     0:00 postgres: checkpointer process                                                                                                                    
 5994 ?        Ss     0:00 postgres: writer process                                                                                                                          
 5995 ?        Ss     0:00 postgres: wal writer process                                                                                                                      
 5996 ?        Ss     0:00 postgres: autovacuum launcher process                                                                                                             
 5997 ?        Ss     0:00 postgres: stats collector process                                                                                                                 
 7059 pts/2    S+     0:00 grep --color=auto postg
16895 ?        S      0:00 su postgres sh -c /usr/lib/postgresql/9.3/bin/postgres           -D  /var/lib/postgresql/9.3/main  -c config_file=/etc/postgresql/9.3/main/postgresql.conf  -c listen_addresses=*
16896 ?        Ss     0:00 /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf -c listen_addresses=*
16898 ?        Ss     0:00 postgres: checkpointer process                                                                                                                    
16899 ?        Ss     0:00 postgres: writer process                                                                                                                          
16900 ?        Ss     0:00 postgres: wal writer process                                                                                                                      
16901 ?        Ss     0:00 postgres: autovacuum launcher process                                                                                                             
16902 ?        Ss     0:00 postgres: stats collector process                                                                                                                 
30751 ?        S      0:00 su postgres sh -c /usr/lib/postgresql/9.3/bin/postgres           -D  /var/lib/postgresql/9.3/main  -c config_file=/etc/postgresql/9.3/main/postgresql.conf  -c listen_addresses=*
30752 ?        Ss     0:00 /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf -c listen_addresses=*
30754 ?        Ss     0:00 postgres: checkpointer process                                                                                                                    
30755 ?        Ss     0:00 postgres: writer process                                                                                                                          
30756 ?        Ss     0:00 postgres: wal writer process                                                                                                                      
30757 ?        Ss     0:00 postgres: autovacuum launcher process                                                                                                             
30758 ?        Ss     0:00 postgres: stats collector process     

The app name is 'dev' since I want to see it on dev.mydomain.com, the pg logs:

root@catchtalk:/var/lib/dokku/plugins# dokku postgresql:logs dev
2014-11-04 06:27:47 UTC LOG:  database system was interrupted; last known up at 2014-11-04 06:07:59 UTC
2014-11-04 06:27:47 UTC LOG:  database system was not properly shut down; automatic recovery in progress
2014-11-04 06:27:47 UTC LOG:  record with zero length at 0/1782E70
2014-11-04 06:27:47 UTC LOG:  redo is not required
2014-11-04 06:27:47 UTC LOG:  database system is ready to accept connections
2014-11-04 06:27:47 UTC LOG:  autovacuum launcher started

One thing I know is different though is that I have set only the A record of dev.myserver.com, not the myserver.com itself, since there is still the old app running on heroku which I'll then want to switch to DO, when the deploy is successful.

But that shouldn't affect anything, right?

1 Answer

Using the Precompile.ignore workaround I've been finally able to deploy my app.

http://stackoverflow.com/questions/7430578/how-to-universally-skip-database-touches-when-precompiling-assets-on-heroku

Quoting the answer:

Add the following as /config/initializers/precompile.rb:

module Precompile

  # Public: ignore the following block during rake assets:precompile
  def self.ignore

    unless ARGV.any? { |e| e == 'assets:precompile' }
      yield
    else
      line = caller.first
      puts "Ignoring line '#{line}' during precompile"
    end

  end

end

and use it in your routes.rb like this:

Precompile.ignore { ActiveAdmin.routes(self) }

Update: Problem is, the server starts, but still no connection :(

Update 2: Removing config/database.yml from git helped (I added it to see if it would help).

Have another answer? Share your knowledge.