ruby-on-railspostgresqlcloud9c9.io

Cloud9 + rails + Postgresql usage


I can not set up a Rails app using Postgresql for development on Cloud9 (c9.io): the migration does not succeed.

Common error:

~/workspace (master) $ rake db:migrate
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
        Is the server running on host "0.0.0.0" and accepting
        TCP/IP connections on port 5432?

Solution

  • Cloud9 does not run PG by defalut. Below is the fast & easy way I use to use Postgresql on C9:

    1. Gemfile.rb:

    gem 'pg'
    

    2. Database.yml:

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      username: my_name
      password: my_pass
      host:     <%= ENV['IP'] %>
    
    development:
      <<: *default
      database: my_db_development
    
    test:
      <<: *default
      database: my_db_test
    
    production:
      <<: *default
      database: my_db_production
    
    1. Paste the following code alltogether in the console:

    `

    sudo service postgresql start
    sudo sudo -u postgres psql
    CREATE USER my_name SUPERUSER PASSWORD 'my_pass';
    \q
    echo "export USERNAME=my_name"
    echo "export PASSWORD=my_pass"
    source
    bundle
    sudo sudo -u postgres psql
    UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
    DROP DATABASE template1;
    CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
    UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
    \c template1
    VACUUM FREEZE;
    \q
    bundle exec rake db:create
    rake db:migrate
    

    Done! However after not using the app for some hours the db goes to sleep & you have to "switch" Postgres on manually by typing in the console: sudo service postgresql start