Question

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?

Was it helpful?

Solution 7

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!

OTHER TIPS

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.

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.

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.

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.

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

@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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top