How to fix the "Role doesn't exist" error? - ruby-on-rails

I just created a new Ruby on rails project using this, in order to deploy it with Heroku
rails new -d postgresql LG_1
Then I used rails s to run my server and found this error :
role "esteban" does not exist.
I've been looking for solutions for an hour, but none of them worked. Using createuser just gives me the does-not-exist error, and I'm not very comfortable with Rails.
The versions I'm using :
Ruby 2.4.4
Rails 5.2.3
Pg (gem) 1.1.4
Here is my database.yml file (without the 75 lines of comments) :
default: &default
adapter: postgresql
encoding: unicode
host: /var/run/postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: LG_1_development
test:
<<: *default
database: LG_1_test
production:
<<: *default
database: LG_1_production
username: LG_1
password: <%= ENV['LG_1_DATABASE_PASSWORD'] %>
I did not edit this file.
EDIT : I found the solution. I had to open the pg_hba.conf file and manually add a new user called "esteban"

Did you install new Gem for project?
If you did I think what you didn't database migration.
Please run following commands.
$heroku run rake db:migrate
$heroku ps:scale web=1
$heroku ps
$heroku open

I guess esteban is the user name on your local machine and because you dont set specific username and password in database.yml for development mode postgres uses username of your user when trying to open connection to database.
Create role esteban in postgres:
# go to psql console under default postgres user
psql postgres
# create role
create role esteban SUPERUSER LOGIN;
# exit psql
\q
Or set correct one in database.yml if you already have it

Related

ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist when I try to migrate bd in rails

I cloned a friend's git repo and I'm trying to migrate the db. I started postgres, but when I run rails db:migrate, I keep getting the errors:
Rails Error: Unable to access log file.
and
ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist
I've tried all available solutions online but keep getting the same error. Does anyone know what I'm doing wrong?
Try the following
Setting Up Postgres
Create a Postgres user for the Rails app we'll create in the next step. To do this, switch into the Postgres user:
su - postgres
Then create a user (or a "role", as Postgres calls it):
create role myapp with createdb login password 'password1';
and make sure you have config/database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
The solution for me was:
createuser -s postgres
After I ran that on the command line my rake task to create the DB worked

Cloning Heroku postgresql DB to local db for "live" testing

So I've followed all of the guides I can find online for this issue, including similar stack posts, but things are not working.
I ran the command
heroku config:get DATABASE_URL
and got
postgres://<username>:<password>#<host_name>:<port>/<dbname>
(with all of the <> parts being my DB info of course) and ran
pg_dump --host=<host_name> --port=<port> --username=<username> --password <dbname> > output.sql
At this point I needed to run
psql -d <local_database_name> -f output.sql
but my rails app followed the Michael Hartl tutorial which uses sqlite3, so I had to switch things over. I tried this by changing my database.yml file to
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
encoding: unicode
# adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development
# database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test
# database: db/test.sqlite3
production:
<<: *default
database: db/production
# database: db/production.sqlite3
I also changed the Gemfile to use 'pg' instead of 'sqlite3' in development. At this point I ran bundle install and tried rails db:setup and rails db:migrate which did not create the databases (.. uh oh) and when I run the final step of
psql -d db/development -f db/output.sql
I get plenty of console output, but when I run a rails console sandbox, there is no user info from my Heroku database. Did I miss a step? What's going on? (I'm working on Ubuntu 14.04)
Edit:
First, I undid all of the local changes I made above. Then, I ended up following a combo of the above and the guide here http://manuelvanrijn.nl/blog/2012/01/18/convert-postgresql-to-sqlite/. I used heroku config:get DATABASE_URL to get all of the needed host, port, username, etc. info and then pulled the data with
pg_dump --host=<host_name> --port=<port> --username=<username> --password --data-only --inserts <dbname> > dump.sql
After that I followed the postgresql to sqlite database conversion (the "Longer story a.k.a please explain a little more." version) I linked above. I did all of the edits described in step 2 as well as having to remove a bunch of SELECT statements at the end of dump.sql. Another change I had to make was replacing all INSERT INTO instances with INSERT OR REPLACE INTO to satisfy some unique constraint errors in step 4. That did it!

fe_sendauth: no password supplied error after setting up PostgreSQL database on Cloud9 IDE

So I've set up a PostgreSQL database on my cloud9 IDE account according to the instructions of the top voted comment: Cloud9 postgres.
This is what my database.yml file looks like:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
host: <%= ENV['IP'] %>
development:
<<: *default
database: sample_app_development
test:
<<: *default
database: sample_app_test
production:
<<: *default
database: sample_app_production
I then created a user model, migrated it, and reloaded my app. However when I loaded the app on the local server using this command: rails server -b $IP -p $PORT on the cloud9 command line, I get the following error message: fe_sendauth: no password supplied error. I do not understand what is going wrong since I have created a superuser with a password and have included those in my database.yml file. The only thing I found was that this error can be fixed by changing the pg_hba.conf file to trust the local server. However I thought the whole point of creating a superuser and password in the Cloud9 IDE was so that you don't have to do this. Also Cloud9 doesn't give you access to the pg_hba.conf file.
You don't need to change the pg_hba.conf file.
Just type in these commands in the terminal:
$ source ~/.profile
$ rake db:create
$ rake db:migrate
Then restart the server normally.
I know you already ran the source ~/.profile command at the beginning of the instructions you mentioned (I have used the answer before and struggled with the same issue) but for some mysterious reason Cloud9 environment (I never had to to do any of this on my Mac) requires to manually source the '.profile' file again after doing the setup specified in those instructions (Cloud 9 just decides to be weird sometimes - like all machines... duh!). The second command creates the new pg database in your development environment with all the configuration from your pg gem and your database.yml file.
The final 'migrate' command might not be necessary but again for mysterious reasons I have also had database issues fixed after just running a 'rake db:migrate' (If I am correct it just updates your database to be able to use your Models or any new dummy data you might have added to your development environment).
Hope this helps.

No password Supplied, but no changes made

I just opened an app I haven't worked on in a while to get back to work. When I tried opening the app in the browser on my local machine, I get this error PG::ConnectionBad (fe_sendauth: no password supplied):. I haven't messed with it lately, so I'm not sure what might have broken. Here's my database.yml (just default rails created verbage)
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
host: localhost
development:
<<: *default
database: project2_development
Any ideas what might have broken? Like I said, I haven't changed anything recently with this app, so I'm not sure what's going on.
The error clearly shows that postgresql the password you have not supplied, and after successful installation of gem 'pg'
perform the following,
sudo -i -u postgres
you will be as a user of postgres eg:- postgres#user >
psql
you will login in postgresql console
alter user user_name with password 'new_password';
will change the password as provided
\q
exit the psql console
change the database.yml as
username: 'postgres
password: 'new_password'
rake db:create
now try to create the database from rails
Should work !

Working on a rails app locally with a remote Postgres connection?

Is there a way to configure the database.yml file to connect to Heroku's Postgres remotely?
I'm having trouble understanding how Heroku, Rails and PG gem work together.
It looks like during deployment, Heroku re-writes the database.yml file - is it possible to see the contents of this updated .yml file and use it locally?
Below are the steps to access Heroku db from local development:
Login to your heroku account.
Navigate to this URL https://postgres.heroku.com/databases/ OR you can get heroku db url by running this command in the terminal: heroku pg:credentials:url
Select your application database.
You will able to see your heroku pg credentials:
Host xxxxxxxxx.77777.amazonaws.com
Database 42feddfddeee
Username 44444444444
Port xxxx
Password 777sfsadferwefsdferwefsdf
collect above details and put in your databse.yml file development evn:
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Restart you application,
Best of luck..!!
I am not a postgres user but this should work.
You can alter your database.yml to include host and port
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
host: heroku.host.name
port: <postgres listen port>
And of course, you should allow connection on the server side, both at the firewall level and database level.
A simple hack to see contents of #{Rails.root}/config/database.yml is to write code to load this yml into an object, then print it out on the UI
DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
puts DB_CONFIG["production"].inspect # or what ever method you want to print it
I am a bit of a newbie and may have misunderstood your question - but. Developed a ROR application using postgress database. Then uploaded to Heroku. I ran DB/Migrate/schema.rb to set up the remote Postgresql database.
From memory heroku run rake db:init (but I could be wrong). Whenver I update the database in develpment to get update in Heroku I have to promote code and run heroku run rake db:migrate
From my config/dtabase.yml
development:
adapter: postgresql
encoding: unicode
database: di_development
pool: 5
username: davidlee
password:
test:
adapter: postgresql
encoding: unicode
database: di_test
pool: 5
username: davidlee
password:
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
and it works. I can't remember doing anything else.
Pierre

Resources