user3408293
user3408293

Reputation: 1395

PG::ConnectionBad FATAL: role "Myname" does not exist

I am trying to use PostgreSQL so that I can deploy to Heroku. However I cannot run localhost anymore why? I get the following message:

PG::ConnectionBad
FATAL: role "Myname" does not exist

Here is my databse.yml

development:
  adapter: postgresql
  database: my_database_development
  pool: 5
  timeout: 5000

test:
  adapter: postgresql
  database: my_database_test
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: my_database_production
  pool: 5
  timeout: 5000 

Here is my gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'

# Use pg as the database for Active Record
gem 'pg'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

gem 'rails_12factor', group: :production

It seems that pg needs me to create a user or databse however I am unable to or don't know how. Couldn't find any commands that worked for me(I'm on a windows btw)

What can I do?

Upvotes: 30

Views: 26960

Answers (9)

Raghvendra Rao
Raghvendra Rao

Reputation: 156

Creating postgres user didn't work for me as mentioned in other answers. Try if you could do psql in your repo. If not try exporting your database

export PGDATABASE=postgres PGHOST=localhost PGUSER=deploy PGPASSWORD=pass

This fixed it for me.

Upvotes: 0

Pawan Bharti
Pawan Bharti

Reputation: 49

Use this command to use your Postgres Shell:

sudo su - postgres

Now use this command to create a user:

createuser -s -r 'user_name'

Now logout.

The error is resolved.

Upvotes: 3

Jared Menard
Jared Menard

Reputation: 2756

I had the same error. For me, I had installed postgresql@10 via home-brew. What I did was run

initdb -D /what/ever/path/you/choose

to set up postgres and then I started postgres in my terminal via

postgres -D /that/same/path/used/in/initdb

Upvotes: 0

Meekohi
Meekohi

Reputation: 10892

What worked for me was: createuser -P -d -e Myname.

-P  If given, createuser will issue a prompt for the password of the new user.
      This is not necessary if you do not plan on using password authentication.
-d  The new user will be allowed to create databases.
-e  Echo the commands that createuser generates and sends to the server.

If you install Postgresql with homebrew on OSX, there is no default postgres user, and you won't be able to use psql directly without setting up a user first.

Upvotes: 12

errakeshpd
errakeshpd

Reputation: 2562

The error is "role "Myname" does not exist",

create the user "Myname" for Postgresql

sudo -u postgres createuser --superuser Myname

it will solve this issue.

Upvotes: 64

user3408293
user3408293

Reputation: 1395

I had to go into my PG admin dashboard and create a db/user there. Unfrotunately it was in a subdirectory different from what the tutorials online said(probably updated directory destination last update). Fortunately I was able to find it and create the table/user there, update my database.yml file and then my app was able to work!

Upvotes: 0

manu29.d
manu29.d

Reputation: 1568

On Windows, I believe it is a little easier.

Install postgresql and PGAdmin for your system. See this

Create a user named postgres and give it a password. You will be nicely prompted to do this.

Then, when you want to create databases, just right click on your connection and choose New Database. The names of these databases should correspond to what is written in your database.yml

Run rake db:migrate RAILS_ENV=development (development| test| production).

These steps worked for me.

Upvotes: 2

Pavan
Pavan

Reputation: 33542

You should create a username and password for your Postgresql

Try creating a user with password in psql

CREATE USER Myname WITH PASSWORD 'your_password';

And your should add those to your database.yml as

username: Myname
password: your_password

Upvotes: 1

Addicted
Addicted

Reputation: 749

@user3408293

  1. After the installation create a user for postgresql

    sudo -u postgres createuser --superuser $USER

    sudo -u postgres createuser pgs_root

  2. Set user password for the postgresql user

    sudo -u postgres psql postgres

    ( For psql prompt) postgres=# \passsword for ex.- postgres=# \passsword pgs_root

N.B You should also add username and password to different environments in database.yml file.

You can also refer this link: Rails: Error installing pg gem

Upvotes: 0

Related Questions