Ident authentication failed for user - ruby-on-rails

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/

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-#

Reliable way of getting the full container name inside a container running in Docker Swarm

Background: I have a setup where many different scalable services connect to their databases via a connection pool (per instance). These services run within a Docker Swarm.
In my current database setup, this ends up looking as follows (using PostgreSQL in this example):
PID | Database | User | Application | Client | ...
... | db1 | app | --standard JDBC driver string-- | x | ...
... | db1 | app | --standard JDBC driver string-- | y | ...
... | db1 | app | --standard JDBC driver string-- | y | ...
... | ... | app | ... | x | ...
... | ... | app | ... | x | ...
... | db2 | app | --standard JDBC driver string-- | y | ...
... | db2 | app | --standard JDBC driver string-- | y | ...
... | ... | app | ... | x | ...
... | ... | app | ... | x | ...
What I would like to do, effectively, is provide the current Docker Swarm container name, including scaling identifier, to the DBMS to be able to better monitor the database connections, i.e.:
PID | Database | User | Application | Client | ...
... | db1 | app | books-service.1 | x | ...
... | db1 | app | books-service.2 | y | ...
... | db1 | app | books-service.3 | y | ...
... | ... | app | ... | x | ...
... | ... | app | ... | x | ...
... | db2 | app | checkout-service.2 | y | ...
... | db2 | app | checkout-service.2 | y | ...
... | ... | app | ... | x | ...
... | ... | app | ... | x | ...
(obviously, setting the connection string is trivial - it's getting the information to set that is the issue)
Since my applications are managed by Docker Swarm (and sometime in the future, likely Kubernetes), I cannot manually set this value via environment (as I do not perform a docker run).
When running docker ps on a given Swarm node, I see the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59cdbf724091 my-docker-registry.net/books-service:latest "/bin/sh -c '/usr/bi…" 7 minutes ago Up 7 minutes books-service.1.zabvo1jal0h2xya9qfftnrnej
0eeeee15e92a my-docker-registry.net/checkout-service:latest "/bin/sh -c 'exec /u…" 8 minutes ago Up 8 minutes checkout-service.2.189s7d09m0q86y7fdf3wpy0vc
Of note is the NAMES column, which includes an identifier for the actual instance of the given container (or image, however you'd prefer to look at it). Do note that this name is not the hostname of the container, which by default is the container ID.
I know there are ways to determine if an application is running inside Docker (e.g. using /proc/1/cgroup), but that doesn't help me either as those also only list the container ID.
Is there a way to get this value from inside a Docker container that is being run in a swarm?
What you need (books-service.1) is a combination of a swarm service name and a task slot. Both of these can be passed to the container as environment variables, as well as a full task name (books-service.1.zabvo1jal0h2xya9qfftnrnej):
version: "3.0"
services:
test:
image: debian:buster
command: cat /dev/stdout
environment:
SERVICE_NAME: '{{ .Service.Name }}'
TASK_SLOT: '{{ .Task.Slot }}'
TASK_NAME: "{{ .Task.Name }}"
After passing these you can either strip the TASK_NAME to get what you need or combine the SERVICE_NAME with the TASK_SLOT. Come to think of it, you can combine them right in the template:
version: "3.0"
services:
test:
image: debian:buster
command: cat /dev/stdout
environment:
MY_NAME: '{{ .Service.Name }}.{{.Task.Slot}}'
Other possible placeholders can be found here.

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.

Postgres & Rails – FATAL: password authentication failed for user

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.

Resources