'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?
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
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.
Solution 4:[4]
Use this command to use your Postgres user:
sudo su - postgres
Now use this command to create a user:
createuser -s -r 'user_name'
Now logout.
The error is resolved.
Solution 5:[5]
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
Solution 6:[6]
@user3408293
After the installation create a user for postgresql
sudo -u postgres createuser --superuser $USER
sudo -u postgres createuser pgs_root
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
Solution 7:[7]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | errakeshpd |
| Solution 2 | |
| Solution 3 | manu29.d |
| Solution 4 | Pawan Bharti |
| Solution 5 | |
| Solution 6 | Community |
| Solution 7 | Jared Menard |
