Password authentication failing for Postgresql - ruby-on-rails

I'm creating a Rails app and using a Postgresql database with it. I've created a few tables and a user, core, which is the owner of each of the tables.
postgres=# create user core with password 'n7zD5FG5';
CREATE ROLE
postgres=# create database core_apps_prod with owner core;
CREATE DATABASE
postgres=# create database core_apps_dev with owner core;
CREATE DATABASE
postgres=# create database core_apps_test with owner core;
CREATE DATABASE
And my database.yml file:
development:
adapter: postgresql
database: core_apps_dev
username: core
password: n7zD5FG5
host: localhost
However, when I run rake db:migrate, I get the error
rake aborted!
FATAL: password authentication failed for user "core"
I also cannot connect to psql manually: psql -U core -W -d core_apps_dev - I get the same error.
How can I allow core to connect to Postgresql on localhost?
The output of SELECT * FROM pg_roles where rolname='core'; is:
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolconfig | oid
---------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+---------------+-----------+-------
core | f | t | f | f | f | t | f | -1 | ******** | | | 16392

You need to add privileges to the role when you create it. To allow logging in and using rake db:create use the following:
create role core with login createdb password 'n7zD5FG5';
To fix your problem, try altering the role to allow login:
alter role core with login;
And if that doesn't work, see if making the login valid forever works:
alter role core valid until 'infinity';
More about roles: http://www.postgresql.org/docs/8.2/static/sql-createrole.html
Edit:
I also had to do this when I installed postgres:
$ psql postgres -c 'CREATE EXTENSION "adminpack";'

The issue turned out to be an issue with the port. The Postgresql Activerecord adapter defaults to port 5432, while the port in my configuration was port 5433. My database.yml now looks like this:
development:
adapter: postgresql
database: core_apps_dev
username: core
password: n7zD5FG5
host: localhost
port: 5433

The newly created user probably is lacking database privileges. There are many different privileges you can give to a user.
Assuming that you have admin account access to psql, go into psql and run
GRANT ALL PRIVILEGES ON DATABASE core_apps_dev to core;

Can you please try
alter role core with password "n7zD5FG5";

Related

Why can't I run migrations on my test database?

I'm trying to run my RoR app in test environment, but I have a problem with migrations on my test database.
My database.yml file looks like this:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: questionnaires-development
test:
<<: *default
database: questionnaires-test
Now I create my databases with that commands:
sudo -u postgres createdb -O karol questionnaires-development
sudo -u postgres createdb -O karol questionnaires-test
Next I run a migration for the development database:
rails db:migrate
I can easily see it works:
== 20180719143415 CreateQuestions: migrating ==================================
-- create_table(:questions)
-> 0.0894s
== 20180719143415 CreateQuestions: migrated (0.0896s) =========================
== 20180722122658 CreateQuestionnaires: migrating =============================
-- create_table(:questionnaires)
-> 0.1050s
== 20180722122658 CreateQuestionnaires: migrated (0.1051s) ====================
Database migrated.
I can also list my tables with Postgres CLI, everything nice:
questionnaires-development=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+-------
public | ar_internal_metadata | table | karol
public | questionnaires | table | karol
public | questions | table | karol
public | schema_migrations | table | karol
(4 rows)
But now I'd like to do the same with my questionnaires-test database. So I run that command:
rails db:migrate RAILS_ENV=test
But results are worrisome:
Database migrated.
Trying to list my tables my worries get confirmed:
questionnaires-test=# \dt
No relations found.
So, what am I doing wrong?

Configuring postgresql in rails

I am working on a project with a friend. I cloned the application from bitbucket. Everything was fine except postgresql (v9.3.7) . It keeps giving me the following message.
psql: FATAL: password authentication failed for user "ubuntu"
FATAL: password authentication failed for user "ubuntu"
I have created a superuser as well as all the databases. The designated users and the list of all the databases is given below.
ubuntu=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
divjot | Superuser | {}
postgres | Superuser, Create role, Create DB, Replication | {}
ubuntu | Superuser, Create role, Create DB | {}
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------------+----------+-----------+---------+-------+-----------------------
app_development | ubuntu | SQL_ASCII | C | C |
app_production | ubuntu | SQL_ASCII | C | C |
app_test | ubuntu | SQL_ASCII | C | C |
postgres | postgres | SQL_ASCII | C | C |
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
ubuntu | ubuntu | SQL_ASCII | C | C |
I have always struggled with postgresql configuration in rails. I follow the documentation completely, however, every time I try to clone application or move the code, I always run into problems. I am not sure why I am getting this error. Any help regarding this would be greatly appreciated. Thanks!!
Disclaimer: I'm not an expert on pgsql. But I've successfully set up pg/rails in several versions and environments. Here's what I suggest.
Find the pg_hba.conf file. Since you are apparently using Ubuntu, try
cd /
find . -name pg_hba.conf -print 2> /dev/null
This will search your whole disk, which will take a while. But eventually it will provide the path. If it produces more than one, you'll have to resolve which one is correct.
If you know where PG is installed, cd there instead of root.
Now
Verify the auth method for user ubuntu is password or maybe md5. Here is the relevant docs page. If you're interested only in local development, you can change password to trust. But if this is for production, that's a bad idea.
While logged into the pg command line, run
ALTER USER ubuntu PASSWORD 'newpassword';
to ensure the password is what you think it is.
You should post database.yaml or ENV['DATABASE_URL'] settings. In general, database.yaml needs to match precisely what pg expects.
For example:
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: ubuntu
password: <your password>
allow_concurrency: true
Caveat: Don't commit production passwords to your repos or even dev passwords if you don't totally control the repo.

Why "host:localhost" must be deleted from database.yml under Cent OS 6, PostgreSQL 9.4 and Rails 3.2, or get a error: Ident authentication failed?

All config files described here are the same as my Mac OS's and all works fine in Mac OS.
I got the same error in CentOS 6 x86_64:
Ident authentication failed for user 'abelard'
When running the following two commands:
1. rake db:create
2. psql -d testforabelard2 -U abelard -h localhost
I got the same error after trying these answers 1 and 2.
My /var/lib/pgsql/9.4/pg_hba.con's content is as follows:
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
And there is a blank file /var/lib/pgsql/9.4/pg_ident.con
My database.yml's content is as follows:
development:
adapter: postgresql
encoding: unicode
database: social_stream_development
pool: 5
username: abelard
password: password
host: localhost
port: 5432
I found a resolution: the error disappear after deleting host:localhost from the above database.yml. But I can not delete host:localhost because there is a sql_host = localhost generated automatically when using think-sphinx for full-text search.
And for offering the same params as my Mac OS's, I altered PostgreSQL's user abelard :
testforabelard2=# \du
List of roles
Role name | Attributes | Member of
-----------+-------------+-----------
abelard | Superuser | {}
: Create role
: Create DB
postgres | Superuser | {}
: Create role
: Create DB
And I can run the command without -h localhost successfully:
psql -d testforabelard2 -U abelard
I don't know what things I miss, what should I do for correct this error? Any advice will be welcome!
I finally resolved myself easily through moving /var/lib/pgsql/9.4/pg_hba.con to /var/lib/pgsql/9.4/data/pg_hba.con.
The reason for this mistake I made is that I referred to my Mac OS's position of the file pg_hba.con.
Of course, I thank this early blog “FATAL: IDENT AUTHENTICATION FAILED”, OR HOW COOL IDEAS GET BAD USAGE SCHEMAS , which reminded me to realise the wrong place of the above file!

Heroku pg:push gives psql permisison denied error

Whenever I type heroku pg:push to get my local database on heroku. I get the permission denied error. Any idea how I can solve it.
$ heroku pg:push mexico2019 HEROKU_POSTGRESQL_COBALT_URL --app mexican
Error:
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: SQL command failed
pg_dump: Error message from server: ERROR: permission denied for relation actions
pg_dump: The command was: LOCK TABLE public.actions IN ACCESS SHARE MODE
pg_dump: *** aborted because of error
pg_restore: [archiver] input file is too short (read 0, expected 5)
Database.yml
development:
adapter: postgresql
encoding: unicode
host: localhost
username: ram
password: (password)
database: mexico2019
timeout: 5000
production:
adapter: postgresql
encoding: unicode
host: localhost
username: postgres
password: (password)
database: meximexi_pro
pool: 5
timeout: 5000
update:
List of relations
Schema | Name | Type | Owner
--------+------------------------------+----------+-------
public | actions | table | ram
public | actions_id_seq | sequence | ram
public | admins | table | ram
I tried
$ sudo su postgres
postgres=# GRANT ALL PRIVILEGES ON DATABASE mexico2019 TO ram;
GRANT
I just noticed when I type the password wrong it says 'psql: FATAL: password authentication failed for user "alain"', shouldn't it say user ram?
I recommend using PG Backups.

Why I can't work with potgreSQL testdatabase, however development postgreSQL is working?

I'm trying to start to working with PostgreSQL and have some troubles.
I create database for development and it is working. I've already created table and added some objects. I followed this post to create databases - http://blog.deliciousrobots.com/2011/12/13/get-postgres-working-on-ubuntu-or-linux-mint/
Here is the code, what I run to create test_database
denmed#denmed:~/projects/internet_shop$ sudo -u postgres createdb -O denys internet_shop_test
denmed#denmed:~/projects/internet_shop$ psql -d internet_shop_test -U denys -WPassword for user denys:
denmed#denmed:~/projects/internet_shop$ psql -d internet_shop_test -U denys -W
Password for user denys:
psql (9.1.7)
Type "help" for help.
internet_shop_test=>
This means I create database and can work with it or NOT ?
Then, it other console(when I was logged in test database in another console) I run command
rake test
and get this errors( I will cut it ):
Errors running test:units! #<ActiveRecord::StatementInvalid: PG::Error: ERROR:
database "internet_shop_test" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
: DROP DATABASE IF EXISTS "internet_shop_test">
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [ruby
-I"lib:test" -I"/home/denmed/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.0.3/lib" "/home
/denmed/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.0.3/lib/rake/rake_test_loader.rb"
"test/functional/**/*_test.rb" ]>
Ok, it tells me that I'm accessing the database. Then I closed logged in test db terminal and run rake test again and get this:
PG::Error: ERROR: permission denied to create database
...
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode",
"database"=>"internet_shop_test", "pool"=>5, "username"=>"denys",
"password"=>"********"}
...
PG::Error: FATAL: database "internet_shop_test" does not exist
Here is my database.yml for development and test databases:
development:
adapter: postgresql
encoding: unicode
database: internet_shop_development
pool: 5
username: denys
password: ********
test:
adapter: postgresql
encoding: unicode
database: internet_shop_test
pool: 5
username: denys
password: ********
What I'm doing wrong ?
User "denys" does not have permission to create databases. The error is pretty clear,
I don't know if you can tell rails to create the database using a different (superuser) account, or just skip the create database step.
Oh - you don't need the "-W" flag on psql either, it'll ask you for a password if it wants one.

Resources