Install and Configure PostgreSQL

@Inderpreet
Thank you :grin: :grin:

I have installed and configured task and was able to login to DB via user but task was failed with below

Task Status - Failed

postgres service is not up and running on DB Server

root@stdb01 ~]# vi /var/lib/pgsql/12/data/pg_hba.conf
[root@stdb01 ~]# systemctl restart postgresql-12
[root@stdb01 ~]# systemctl restart postgresql-12
[root@stdb01 ~]# psql kodekloud_db4 -U kodekloud_pop
Password for user kodekloud_pop:
psql (12.2)
Type “help” for help.

kodekloud_db4=> thor@jump_host /$ ssh peter@stdb01
peter@stdb01’s password:
Last login: Sun Mar 22 09:42:05 2020 from jump_host.linuxpostgresql_db_net
[peter@stdb01 ~]$ sudo su -
[sudo] password for peter:
Last login: Sun Mar 22 09:42:14 UTC 2020 on pts/0
[root@stdb01 ~]# systemctl status postgres-12
Unit postgres-12.service could not be found.
[root@stdb01 ~]# systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2020-03-22 10:08:37 UTC; 4min 21s ago
Docs: PostgreSQL: Documentation: 12: PostgreSQL 12.16 Documentation
Process: 491 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 496 (postmaster)
CGroup: /docker/5ca52fb7e8d716f1f449a597af2de3bdc8272d9926e3dd067c381ac1a8a952bf/system.slice/postgresql-12.service
├─496 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
├─497 postgres: logger
├─499 postgres: checkpointer
├─500 postgres: background writer
├─501 postgres: walwriter
├─502 postgres: autovacuum launcher
├─503 postgres: stats collector
└─504 postgres: logical replication launcher

Mar 22 10:08:37 stdb01 systemd[1]: Starting PostgreSQL 12 database server…
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.333 UTC [496] LOG: starting PostgreSQL 12.2 on x86_6…64-bit
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.334 UTC [496] LOG: listening on IPv4 address "127.0…t 5432
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.334 UTC [496] LOG: could not bind IPv6 address “::1”…ddress
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.334 UTC [496] HINT: Is another postmaster already ru…retry.
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.335 UTC [496] LOG: listening on Unix socket “/var/ru…5432”
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.337 UTC [496] LOG: listening on Unix socket “/tmp/.s…5432”
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.346 UTC [496] LOG: redirecting log output to logging…rocess
Mar 22 10:08:37 stdb01 postmaster[496]: 2020-03-22 10:08:37.346 UTC [496] HINT: Future log output will appear in…“log”.
Mar 22 10:08:37 stdb01 systemd[1]: Started PostgreSQL 12 d

@Inderpreet
I attempted the question again, this time got error “the service is not up and running”.

Hi @Inderpreet any update on this?

Hi @dipjyoty @vivek.karakandy

Task is marked Success for you guys. Thanks for your patience.

We figured out the issue with validation and its fixed now.

Hi, @Inderpreet thanks for the update, however it still shows the task as failed in my profile.

@dipjyoty Please check now.

@Inderpreet it’s reflecting now. Thank you so much :slight_smile:

Same issue faced … Hope someone could assist and fix.

https://kodekloud.com/community/t/install-configure-postgresql-task/2470/5?u=kleansoul

Hi @kleansoul, in the screenshot you are using the -W switch to connect to the DB, which forces psql to prompt for a password before connecting to a database. Did you enable md5 authentication for the user by changing the configuration in file /var/lib/pgsql/12/data/pg_hba.conf ?

Hi,

Whats wrong with my task resolution

a. Install and configure PostgreSQL database on Nautilus database server.

yum install postgresql-server postgresql-contrib
postgresql-setup initdb
systemctl enable postgresql && systemctl start postgresql

b. Create a database user kodekloud_aim and set its password to GyQkFRVNr3.

echo -n 'GyQkFRVNr3' | md5sum
20c59d9fe9be7db32e683554222f227c

su - postgres
psql
psql=# CREATE USER kodekloud_aim WITH ENCRYPTED PASSWORD 'md520c59d9fe9be7db32e683554222f227c';
psql=# CREATE DATABASE kodekloud_db9 OWNER kodekloud_aim;

c. Create a database kodekloud_db9 and grant full permissions to
user kodekloud_aim on this database.

psql=# GRANT ALL PRIVILEGES ON DATABASE kodekloud_db9 TO kodekloud_aim;

d. Make appropriate settings to allow all local clients to
connect to the kodekloud_db9 database through kodekloud_db9
user using md5 encrypted password for authentication.

edit pg_hba.conf to use md5 and change all to kodekloud_db9
edit postgresql.conf to permit listen = localhost

systemctl restart postgresql.service
systemctl status postgresql.service

e. At the end its good to test the db connection using these new
credentials from root user or server’s sudo user.

try to connect psql using root does not work
    psql -U root -h localhost -d kodekloud_db9 -W 
Failed 

Connect using kodekloud_aim 
    psql -U kodekloud_aim -d kodekloud_db9 -h 127.0.0.1 -W

Works OK

@kleansoul did you also test it with -h localhost ?

Yes, and it worked with that too.

Thats why I was surprised why the task got failed.

@kleansoul
what is your email ID?

Hi @Inderpreet,

The task has also been marked failed for me though I was able to authenticate with password using the kodekloud_roy user to database kodekloud_db8 as described in the task. The error is saying the user or the database does not exist, please check.

[email protected]

Please check. Thank you!

@Inderpreet

Did you got time to check?

Hi @Inderpreet

Havnt received any reply from you yet on that task ??

Have you checked and marked it correctly please.

@kleansoul Sorry for delayed response. There were some missing config changes in your hba conf. Did you enable the local all connections ? As mentioned in last point of question you could test by login into Linux root user or sudo user and then by accessing the DB using credentials you created.