Skip to content

Instantly share code, notes, and snippets.

@eliotsykes
Last active December 20, 2021 13:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save eliotsykes/10cf6f1557556e5cafcc857792e63fff to your computer and use it in GitHub Desktop.
Save eliotsykes/10cf6f1557556e5cafcc857792e63fff 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.

@Somatori
Copy link

Somatori commented Mar 11, 2020

Thanks.
I've setup my Rails app with PostgreSQL and looks like it works fine.

Is it possible to use sort of web-client for PostgreSQL management (like pgAdmin4) on Cloud9?
I've tried to use the guide to setup it there (https://tecadmin.net/install-pgadmin4-on-ubuntu/), but stuck on the point of accessing pgAdmin4 in a web browser - I just don't know the appropriate URL for that...

@Collinsifere
Copy link

After I ran bundle exec rake db:setup, I got the message that the development database is not configured. I'm skeptical of deleting the 'db /squlite file. Do not know if it will be fatal the app.

@Doble-U
Copy link

Doble-U commented Jul 30, 2020

It doesn't work for me.
User is "postgres" or "ubuntu"?

@Doble-U
Copy link

Doble-U commented Jul 30, 2020

I did this:

sudo -u postgres createuser -s ubuntu
sudo -u postgres createdb ubuntu

sudo su postgres
psql
ALTER USER "ubuntu" WITH SUPERUSER;
\q

then, restart pg:

sudo service postgresql start

and voila!

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