Postgresql or psql 'rails db' password error - ruby-on-rails

I am trying to prompt the psql interface to try to create a database, actually following from Dr. Hartl's tutorials http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?version=3.2.
I created the project with:
rails new postgr_ --database=postgresql
I added passwords to the database.yml file:
development:
adapter: postgresql
encoding: unicode
database: postgr__development
pool: 5
username: postgr_
password: 12345
test:
adapter: postgresql
encoding: unicode
database: postgr__test
pool: 5
username: postgr_
password: 12345
production:
adapter: postgresql
encoding: unicode
database: postgr__production
pool: 5
username: postgr_
password: 12345
I then enter into terminal:
$ rails db
And I get the following error after entering my password:
psql: FATAL: password authentication failed for user "postgr_"
I've been going at this a good part of yesterday and all day today and was unable to work around this. I may very well be missing something fundamental, if you spot it please let me know. Thank you!

You're missing the steps to setup the postgresql role and database creation.
This procedure depends on the system you are using. I will assume that you are using a mainstream linux distribution.
First login to the postgresql account. You may use one of the following commands:
$ su - postgres
or
$ sudo -i -u postgres
Once logged in, start the psql program:
$ psql template1
At the psql prompt, create a new user role and a database for your project:
=> create role postgr_ with createdb login password '12345';
Then simply quit the program
=> \q
And logout from the postgresql user account
$ exit
Then you should be able to run the rail db command successfully

Related

How to fix the "Role doesn't exist" error?

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

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

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 !

Rails error creating database with postgres

the first time when postgresql is installed in my local machine i created a user "user1" using the following command :
$ sudo su postgres -c psql
postgres=# CREATE ROLE user1 SUPERUSER LOGIN;
postgres=# \q
then setup my database.yml
development:
adapter: postgresql
encoding: unicode
database: appname_development
pool: 5
timeout: 5000
username: user1
password:
and all work fine, so today i created a new app then i did the same steps (creating a new user "user2" and modify my database.yml)
development:
adapter: postgresql
encoding: unicode
database: application2_development
host: localhost
pool: 5
timeout: 5000
username: user2
password:
but when i try to create the database
$ rake db:create:all
i get this error :
FATAL: Peer authentication failed for user "application2"
i tried to fix the problem by adding host: localhost to my database.yml but now i get another error which is :
fe_sendauth: no password supplied
can someone explains to me what means this error, and why the first time when i created the first user it doesn't show me this error ? then i will be thankful to know the solution as well.
thank you
I have already faced this problem and fixed it.
I think, It will be helpful to you.
please find 'pg_hba.conf' and postgresql.conf files.
generally, pg_hba.conf and postgresql.conf files located at /etc/postgresql/9.1/main/.
put `local all all md5' in pg_hba.conf and save it.
open postgresql.conf file and if listen_addresses = 'localhost' and port = 5432 line are commented. then uncomment to it.
Finally restart to pg server and try to create database.

Travis CI: FATAL: role does not exist

I'm trying to get my application working w/ Travis CI but I keep getting: FATAL: role "skateparks" does not exist. Any ideas on what I could be doing wrong? I've followed their documentation.
For the record, put something like this in your .travis.yml:
before_script:
- psql -c "CREATE USER skateparks WITH PASSWORD 'skateparks';" -U postgres
Your database.yml has this:
development:
adapter: postgresql
encoding: utf8
database: skateparks_development
username: skateparks
password:
template: template0 # Required for UTF8 encoding
Note the username: skateparks part. Either drop that or create the role with something like:
create role skateparks login
from the psql shell.
This worked for me
from bash...
createuser blog
from psql prompt
ALTER USER blog CREATEDB;
my database.yml
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:

Resources