Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pedrolopez/bfd6c078b15b3b5171d7a35abf3f97ae to your computer and use it in GitHub Desktop.
Save pedrolopez/bfd6c078b15b3b5171d7a35abf3f97ae to your computer and use it in GitHub Desktop.
How to setup PostgreSQL & Rails on Cloud9

How to setup PostgreSQL & Rails on Cloud9

At time of writing, Cloud9 has PostgreSQL pre-installed, so you won't need to install it yourself. However, its not running by default, so you will need to start it with this command in the terminal:

sudo service postgresql start

Change the PostgreSQL password to 'password' (or choose a different password):

sudo sudo -u postgres psql

# This will open the psql client.

# Type \password and press enter to begin process
# of changing the password:
postgres=# \password

# Type your new password (e.g. "password") and press enter twice:
Enter new password: 
Enter it again: 

# Password changed, quit psql with \q
postgres=# \q 

Edit your config/database.yml to be:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  
  # Important configs for cloud9, change password value
  # to what you entered in the previous psql step.
  template: template0
  username: ubuntu
  password: password
  
development:
  <<: *default
  database: your_app_name_development

test:
  <<: *default
  database: your_app_name_test

production:
  <<: *default
  database: your_app_name_production
  username: your_app_name
  password: <%= ENV['YOUR_APP_NAME_DATABASE_PASSWORD'] %>

(Note the template, username, and password configs in the default section above are essential).

Add the pg gem to your Gemfile:

gem 'pg'

Run bundle install.

Remove the sqlite gem from your Gemfile (and optionally delete the db/*.sqlite3 files).

Create the database, load schema.rb, and seed the database using the db:setup task:

bundle exec rake db:setup

# Run bin/rake -AD db to see all db-related tasks

Start or restart your rails app and check it is working.

Note, the non-seed data from your old sqlite database will not be present in the new database.

If you ever want to use the psql client to interact with PostgreSQL directly, in the terminal run psql or run bin/rails db.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment