Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Rails install: ActiveRecord::NoDatabaseError FATAL: role does not exist

New to Rails and trying to get a local development environment up and running of a site that's been cloned off of git.

When I start postgres and type 'rails s' in terminal under my project to start the server, I get hit with the following:

=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2017-01-08 20:43:24] INFO  WEBrick 1.3.1
[2017-01-08 20:43:24] INFO  ruby 2.2.6 (2016-11-15) [i386-mingw32]
[2017-01-08 20:43:24] INFO  WEBrick::HTTPServer#start: pid=1036 port=3000




Started GET "/" for ::1 at 2017-01-08 20:43:29 -0500
Started GET "/" for ::1 at 2017-01-08 20:43:29 -0500

ActiveRecord::NoDatabaseError (FATAL:  role "username" does not exist
):

If I try and run

rake db:create

I receive

FATAL:  role "username" does not exist
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in
.....

Basically the same with rake db:migrate as well. All migrations of the site are in \Sites\sitename\sitename\db\migrate

If I try and create the user

psql -U username

I'm immediately returned

psql: FATAL:  role "username" does not exist

I've been wracking my head on this all afternoon.

I'm on a Windows box. Any ideas?

like image 252
user1318340 Avatar asked Jan 09 '17 01:01

user1318340


2 Answers

Firstly you have to set up your database and create super user in psql. Go to psql:

sudo -u postgres psql

Then you can create user with a password. For example:

create user username with password '12345678';

Rails will manage database with help of this user. So we have to create a super user too:

alter user username with superuser;

After you had created a superuser, you can quit from psql. The same data must be in your /config/database.yml file:

development:
  <<: *default
  database: database_name
  username: username
  password: '12345678'

In the your application repositroy create a database with help of rake:

rake db:create
like image 90
StanisLove Sid Avatar answered Oct 30 '22 10:10

StanisLove Sid


Please update your database.yml likewise.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: 'some_name'
  username: 'postgres'
  password: 'postgres'
  host: localhost
  port: 5432

Postgresql while installation comes with default role: 'postgres' with Password: 'postgres'. If you don't want to use it you can create a role and grant them all rights.

CREATE ROLE new_user WITH CREATEDB CREATEROLE SUPERUSER;

Then update your database.yml with the newly created role.

Hope this helps you.

like image 26
RamPrasad Reddy Avatar answered Oct 30 '22 11:10

RamPrasad Reddy