Postgres & Rails – FATAL: password authentication failed for user - ruby-on-rails

Newbie to Postgres, basic skills with Rails, so bear with me.
I will explain the process of how I got to where I am, in case I did anything wrong along the way.
What I did.
I created a Rails 5 project (using the actual Git repo) and, once it was created successfully, I found the default gem sqlite3 in the Gemfile.
Now, I want to work with Postgres, so I turned sqlite3 into pg, and ran bundle install. Worked perfectly, Rails didn't complain.
Then, I installed Postgres on my computer (using the latest OS X) and ran createdb testDB. It's an empty database.
Now, I have to configure the database.yml file. After going through a dozen of links and resources, I changed the file's contents to the following:
default: &default
adapter: postgresql
database: testDB
username: postgres
password: password
host: localhost
port: 9057
development:
<<: *default
test:
<<: *default
production:
<<: *default
database: someOtherDB
I am certain that the port number is correct, because I set it manually, and I can check that it's running using the command netstat -na. I get the following two lines:
tcp6 0 0 *.9057 *.* LISTEN
tcp4 0 0 *.9057 *.* LISTEN
Now, in order to change the password for postgres to ensure it's actually the cleverly secure string password, I ran the following commands:
$ sudo -u postgres psql template1
Opens up the psql prompt, then I run the command:
psql (9.3.4)
Type "help" for help.
template1=# ALTER USER postgres PASSWORD 'password';
Which returned:
ALTER ROLE
Yay! Password's changed! Now I can open up my first ever working Rails app with a Postgres backend!
What I got.
I run bin/rails server, and I get the following error:
Some more info that may be helpful.
When I run the command psql -l, I get the following:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------------+----------+-------------+-------------+-------------------------------
postgres | myusername | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | myusername | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/myusername +
| | | | | myusername=CTc/myusername
template1 | myusername | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/myusername +
| | | | | myusername=CTc/myusername
test | myusername | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
testDB | myusername | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
(5 rows)
If I run psql X (where X is any of the databases listed above) followed by running the command \du, I get the following.
List of roles
Role name | Attributes | Member of
--------------+------------------------------------------------+-----------
myusername | Superuser, Create role, Create DB, Replication | {}
postgres | Superuser | {}
I followed instructions on this link and a few others to no avail.
What am I doing wrong?
Thank you so much in advance.
EDIT:
Here's the content of log/development.log.

The default login for rails is the current username logged in and a blank password.

Related

SQL asking for a password I didn't set

Im learning psql/sql
The problem i am having is the program keeps asking for the password for User testuser2, but I didn't set a password for this User, so I don't know what the password could be. I've tried using the password for User postgress but it still wouldn't work.
Code
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:
psql (14.2)
WARNING: Console code page (850) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+-----------+----------+-----------------------------+-----------------------------+-----------------------
postgres | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 |
room_booking | cs_staff | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 |
template0 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
todolist2 | testuser2 | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 |
(5 rows)
postgres=# todolist2 testuser2
postgres-# \c todolist2 testuser2
Password for user testuser2:
connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "testuser2"
Previous connection kept
postgres-#

PostgreSQL installation issue Character Type

I installed PostgreSQL 9.2 with default database postgres.
It shows Collation and Character Type as en_GB.UTF-8.
Is there any difference between en_GB and en_US. Can I change to en_US without uninstalling.
First option is while creating a new database you may provide the Collation and Ctype like
postgres=# CREATE DATABASE test WITH LC_COLLATE='en_US' Encoding='LATIN1' lc_ctype='en_US' TEMPLATE=template0;;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | LATIN1 | en_US | en_US |
(4 rows)
Second option is to do the fresh initdb and provide the -E encoding and --locale=locale. Sets the default locale for the database cluster. If this option is not specified, the locale is inherited from the environment that initdb runs in
See more details about initdb with --locale at postgres initdb

Kartoza postgis docker image: Psql says role does not exist

I'm trying to run the docker image kartoza/postgis locally on MacOS. It works perfectly when I run it on an Ubuntu instance on DigitalOcean, but when I try to interface with the local container using psql command line interface, I get the following:
Mac:~ User$ sudo psql -h localhost -U docker -p 5432 -l
psql: FATAL: role "docker" does not exist
Where docker is the username I specified when spinning up the container.
Below are the command I used to spin up the container, as well as the logs from the container:
Mac:~ User$ docker run --name "postgis" -p 5432:5432 -e ALLOW_IP_RANGE=0.0.0.0/0 -d -t kartoza/postgis
Logs:
Mac:~ User$ docker logs postgis
Add rule to pg_hba: 0.0.0.0/0
Add rule to pg_hba: replication user
Setup master database
2019-03-11 11:40:40.625 UTC [28] LOG: listening on IPv4 address "127.0.0.1", port 5432
2019-03-11 11:40:40.628 UTC [28] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-03-11 11:40:40.644 UTC [33] LOG: database system was shut down at 2019-02-01 14:24:17 UTC
2019-03-11 11:40:40.649 UTC [28] LOG: database system is ready to accept connections
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres ready
Postgis is missing, installing now
Creating template postgis
Enabling template_postgis as a template
UPDATE 1
Loading postgis extension
CREATE EXTENSION
Enabling hstore in the template
CREATE EXTENSION
Enabling topology in the template
CREATE EXTENSION
Loading legacy sql
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE OPERATOR CLASS
Setup postgres User:Password
CREATE ROLE
Check default db exists
Create default db gis
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privilege
s
------------------+----------+----------+---------+---------+-------------------
----
gis | docker | UTF8 | C.UTF-8 | C.UTF-8 |
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/postg
res
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/postg
res
template_postgis | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
(5 rows)
2019-03-11 11:40:43.331 UTC [28] LOG: received smart shutdown request
2019-03-11 11:40:43.339 UTC [28] LOG: background worker "logical replication launcher" (PID 39) exited with exit code 1
2019-03-11 11:40:43.340 UTC [34] LOG: shutting down
2019-03-11 11:40:43.362 UTC [28] LOG: database system is shut down
/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
Postgres initialisation process completed .... restarting in foreground
2019-03-11 11:40:44.382 UTC [212] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-03-11 11:40:44.382 UTC [212] LOG: listening on IPv6 address "::", port 5432
2019-03-11 11:40:44.387 UTC [212] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-03-11 11:40:44.406 UTC [215] LOG: database system was shut down at 2019-03-11 11:40:43 UTC
2019-03-11 11:40:44.414 UTC [212] LOG: database system is ready to accept connections
As far as I can see the user "docker" is the owner of the database "gis".
Can you set the database "gis":
psql -d gis -U username -h localhost
Then you can give your password.

Ident authentication failed for user

I am trying to set up my rails app with a Postgres database which I am new to.
I am using
centos 6
postgresql 9.6.2
pg_hba.conf path => /var/lib/pgsql/data/pg_hba.conf
I had logged in with sudo -u postgres psql and created a user 'X' with password 'password'. Below are the details
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+--------------+----------+-------------+-------------+-------------------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
trip_staging | X | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/X +
| | | | | X=CTc/X
(4 rows)
postgres=# \du
List of roles
Role name | Attributes | Member of
--------------+------------------------------------------------------------+-----------
X | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
When I run command:
RAILS_ENV=staging bundle exec rake db:create
I get the below error:
FATAL: Ident authentication failed for user "X"
Couldn't create database for {"database"=>"trip_staging", "adapter"=>"postgresql", "username"=>"X", "password"=>"password", "host"=>"localhost", "port"=>5432, "pool"=>5, "timeout"=>5000}
After this I looked up each possible fix for this. I tried changing the pg_hba.conf with below configuration:
TYPE DATABASE USER ADDRESS METHOD
local all all md5
host all all 0.0.0.0 md5
host all all ::1/128 md5
Also tried, changing METHOD with trust and then restarted the postgresql server with the command: sudo service postgresql-9.6 restart but same error.
Also tried, changing read permissions for path /var/lib/pgsql/data/pg_hba.conf from 600 to 755. To do that I had to login as postgres superuser with sudo su - postgres which is weird. Then restarted the postgresql server
Also tried, changing Method from ident to trust for path /var/lib/pgsql/9.6/data/pg_hba.conf, as apparently there exists a version wise pg_hba.conf too. Then I restarted the PostgreSQL server.
Nothing seemed to have worked so far. Please help find out what is missing.
In the pg_hba.conf the part of 0.0.0.0 needs the mask like this 0.0.0.0/32, in the log says the problem, wich is by default in centos in /var/lib/pgsql/9.6/data/pg_log/

Rails - Issue with multiple remote database in production/staging

I’m currently running a Rails app which uses 2 databases: its own and one from another (not running anymore) app on a different server. The thing is: locally everything goes just smooth, but on our staging server after a few requests the page load hangs forever and then returns PG::ConnectionBad error: could not connect to server: Connection timed out Is the server running on host "***" and accepting TCP/IP connections on port ***, which is the second database (running on a different server). I use the second (remote) database for devise authentication. I think it is something related to the connection pool related to the remote database not properly releasing after use, but I really have no clue at the moment.
Rails version: 5.0.0
Postgres version: 9.3
Passenger version: 5.0.29
This is the error thrown:
PG::ConnectionBad - could not connect to server: Connection timed out Is the server running on host "********.cloudapp.net" (********) and accepting TCP/IP connections on port ****?
As I said that happens after a while, so I really think it has to be something related to ActiveRecord connection pool or the connection with the remote database, which is by the way logging this:
2016-07-19 16:49:40 UTC LOG: could not receive data from client: Connection reset by peer
2016-07-19 16:49:40 UTC LOG: incomplete startup packet
2016-07-19 16:50:05 UTC LOG: could not receive data from client: Connection reset by peer
2016-07-19 16:50:05 UTC LOG: incomplete startup packet
More of it, this is the status of the remote Postgres, retrieved by running SELECT * FROM pg_stat_activity;:
datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query
-------+---------------------+------+----------+-------------+-----------------------------------------------------------------+-----------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+--------+-------------------------------------------------------------------------------------------
16733 | ******************* | 7620 | 16584 | *********** | Passenger RubyApp: /var/www/**********/current/public (staging) | *************** | | 1336 | 2016-07-19 16:51:07.955763+00 | | 2016-07-19 16:51:08.015955+00 | 2016-07-19 16:51:08.016093+00 | f | idle | SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2
12035 | postgres | 7673 | 10 | postgres | psql | | | -1 | 2016-07-19 16:52:08.773491+00 | 2016-07-19 16:52:11.052339+00 | 2016-07-19 16:52:11.052339+00 | 2016-07-19 16:52:11.052343+00 | f | active | SELECT * FROM pg_stat_activity;
(2 rows)
As you can see, the first query, coming from my Rails app, remains in idle state, and even if it is one more clue pointing in the connection pool direction I really have no idea how to fix this. Any suggestion would be REALLY appreciated! Restarting Postgres server on the remote server make things work again, so I'm really pointing at Postgres or Rails not releasing connections.
This is the model (User) whose connections are creating trouble:
class RemoteBaseModel < ActiveRecord::Base
self.abstract_class = true
establish_connection :other_db_env
end
class User < RemoteBaseModel
# model logic
end
And this is database.yml:
##### database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 25
development:
<<: *default
database: db_development
staging:
<<: *default
database: db_staging
username: ********
password: ********
other_db_default: &other_db_default
adapter: postgresql
encoding: unicode
pool: 25
fanta_users_development:
<<: *other_db_default
database: other_db_development
other_db_staging:
<<: *other_db_default
host: ********
port: 1972
database: other_db_staging
username: ********
password: ********
protocol: md5

Resources