Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails & Postgresql on AWS Cloud 9 : role "ec2-user" does not exist

I am trying to get my Rails environment up and running with a Postgres database using AWS Cloud9 and have run into a problem when trying run rails db:migrate.

Initially I created the project by running:

  1. rails new app_name -d postgresql
  2. bundle install

Bundler had a problem finding gem 'pg' so I ran:

  1. sudo yum install postgresql-devel
  2. sudo yum install postgresql-server
  3. sudo postgresql initdb
  4. sudo service postgresql start

The server fired up fine afterwards and I thought all was well until running rails db:migrate which returned the error:

PG::ConnectionBad: FATAL: role "ec2-user" does not exist

I am unsure how to fix this.

It has been suggested that I may need to get into my psql shell and alter or create a new role, but I'm unsure how to alter the ec2-user.

It has also been suggested that my pg_hba.conf file may need some alterations. I have the path to that file, but am not sure how to edit it or if that's something that I really want to do.

Any suggestions? I'm including my database.yml below:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: my_app_development

test:
  <<: *default
  database: my_app_test

production:
  <<: *default
  database: my_app_production
  username: my_app
  password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>
like image 925
Belder Avatar asked Jan 11 '18 15:01

Belder


1 Answers

Every user of psql also needs a corresponding database that matches their name.

At the bash command line:

sudo -u postgres createuser -s ec2-user
sudo -u postgres createdb ec2-user

The above should allow your user to access psql, but it won't let rails make migrations yet. You'll have to do the following first:

sudo su postgres
psql
ALTER USER "ec2-user" WITH SUPERUSER;
\q
exit

I put that together quite quickly so let me know if you run into any problems.

If your config/database.yml is using a different login user to your bash user, you should repeat all above steps for that user as well.

Lastly, although you are on Cloud9, this is just a simple Rails/Postgres issue, rather than an AWS issue.

like image 86
FelixFortis Avatar answered Sep 21 '22 03:09

FelixFortis