Day17 : Install and Configure PostgreSQL - postgresql service is failed

Hi Team, Can you please resolve lab setup issue in Day17.

the PostgreSQL service is failing to start in the environment. The instructions mention that PostgreSQL should already be installed and running and also specify not to restart the service. Since the service is failing before I can run any commands, I’m unable to complete the task.

Could you please check and fix the lab setup?

image

Hi @Anubhav_Swastik

Postgres is installed on the DB Server, But the DB Dir is not initialized, without which Postgres Service won’t start.

I am not sure if initializing the DB Dir is part of the task. I’ll confirm with the team of this.

I tried this by initializing the PGDATA dir, and it passed for me.

1 Like

Hi @Santosh_KodeKloud Thanks Issue is resolved.

@Santosh_KodeKloud, how did you initialize the DB? Because it tried to initialize with this commands

# su - postgres
$ pg_ctl initdb

Create a user and DB. But after checking, I failed the task

Here is what I did:
[peter@stdb01 ~]$ sudo su - postgres
[postgres@stdb01 ~]$ psql
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
[postgres@stdb01 ~]$ pg_ctl initdb
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.

The database cluster will be initialized with locale “C.utf8”.
The default database encoding has accordingly been set to “UTF8”.
The default text search configuration will be set to “english”.

Data page checksums are disabled.

creating directory /var/lib/pgsql/data … ok
creating subdirectories … ok
selecting dynamic shared memory implementation … posix
selecting default max_connections … 100
selecting default shared_buffers … 128MB
selecting default time zone … Etc/UTC
creating configuration files … ok
running bootstrap script … ok
performing post-bootstrap initialization … ok
syncing data to disk … ok

initdb: warning: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

/usr/bin/pg_ctl -D /var/lib/pgsql/data -l logfile start

[postgres@stdb01 ~]$ /usr/bin/pg_ctl -D /var/lib/pgsql/data -l logfile start
waiting for server to start… done
server started
[postgres@stdb01 ~]$ psql
psql (13.14)
Type “help” for help.

postgres=# CREATE USER kodekloud_tim WITH PASSWORD ‘LQfKeWWxWD’;
CREATE ROLE
postgres=# CREATE DATABASE kodekloud_db10;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE kodekloud_db10 TO kodekloud_tim;
GRANT
postgres=# \l

Hello @Anubhav_Swastik ,
How did you initialize the DB?

Everything is completed, but the lab still fails

[postgres@stdb01 ~]$ initdb -D /var/lib/pgsql/data
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.

The database cluster will be initialized with locale “C.utf8”.
The default database encoding has accordingly been set to “UTF8”.
The default text search configuration will be set to “english”.

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/pgsql/data … ok
creating subdirectories … ok
selecting dynamic shared memory implementation … posix
selecting default max_connections … 100
selecting default shared_buffers … 128MB
selecting default time zone … Etc/UTC
creating configuration files … ok
running bootstrap script … ok
performing post-bootstrap initialization … ok
syncing data to disk … ok

initdb: warning: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

pg_ctl -D /var/lib/pgsql/data -l logfile start

[postgres@stdb01 ~]$ psql
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
[postgres@stdb01 ~]$ pg_ctl -D /var/lib/pgsql/data -l logfile start
waiting for server to start… done
server started
[postgres@stdb01 ~]$ psql
psql (13.14)
Type “help” for help.

postgres=# CREATE USER kodekloud_rin WITH PASSWORD ‘dCV3szSGNA’;
CREATE ROLE
postgres=# CREATE DATABASE kodekloud_db1;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE kodekloud_db1 TO kodekloud_rin;
GRANT
postgres=# \q
[postgres@stdb01 ~]$ cd /var/lib/pgsql/data/
[postgres@stdb01 data]$ ls
PG_VERSION pg_dynshmem pg_replslot pg_tblspc postmaster.opts
base pg_hba.conf pg_serial pg_twophase postmaster.pid
current_logfiles pg_ident.conf pg_snapshots pg_wal
global pg_logical pg_stat pg_xact
log pg_multixact pg_stat_tmp postgresql.auto.conf
pg_commit_ts pg_notify pg_subtrans postgresql.conf
[postgres@stdb01 data]$

Hi @Kasthuri initialize the db and then start the postgresql service
postgres initdb -D /path/to/your/data/directory
once the service is up and running
Switch to the postgres user and perform the task

@Anubhav_Swastik resolved. Thank you!

Hi @Kasthuri, may i know how to solve this issues please?
I tried as per your command and all fine but lab failed.

Hi @allforum.kp
I followed the same as mentioned by anubhav, instead of running pg_ctl -D /var/lib/pgsql/data -l logfile start
I don’t know if that was the right way, but I initialised the DB in the postgres user, and started and enabled the postgres in the root.

Again came to PostgreSQL user to do all other operations related to the task.

Only this method helped me to pass.

1 Like

Hello @Kasthuri thanks for your reply.
I also done the lab. My way a little bit messy, including removing data directory, recreating data, changing owner… then I can start the postgresql.service and my lab was fine. Anyway, thank you. Have a nice day.

@Kasthuri @Anubhav_Swastik @Halinum97 Hi Team, I am using the following steps but still getting task failed error

Task is failed with the error:

- package 'postgresql' package is not installed or service 'postgresql' is not up and running on DB Server
thor@jumphost ~$ ssh peter@stdb01
[peter@stdb01 ~]$ sudo su - postgres
[postgres@stdb01 ~]$ initdb -D /var/lib/pgsql/data   --> If error
[postgres@stdb01 ~]$ rm -rf /var/lib/pgsql/data/* 
[postgres@stdb01 ~]$ sudo killall -9 postgres
[postgres@stdb01 ~]$ initdb -D /var/lib/pgsql/data
[postgres@stdb01 ~]$ pg_ctl -D /var/lib/pgsql/data -l logfile start

[postgres@stdb01 ~]$ psql 
psql (13.14)
Type "help" for help.

postgres=# psql -c "CREATE USER kodekloud_cap WITH PASSWORD 'YchZHRcLkL' SUPERUSER;"
postgres-# psql -c "CREATE DATABASE kodekloud_db2;"
postgres-# psql -c "GRANT ALL PRIVILEGES ON DATABASE kodekloud_db2 TO kodekloud_cap;"
postgres-# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype  |   Access privileges   
-----------+----------+----------+---------+--------+-----------------------
 postgres  | postgres | UTF8     | C.utf8  | C.utf8 | 
 template0 | postgres | UTF8     | C.utf8  | C.utf8 | =c/postgres          +
           |          |          |         |        | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.utf8  | C.utf8 | =c/postgres          +
           |          |          |         |        | postgres=CTc/postgres
(3 rows)

postgres-# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+--------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

I did this task 2 days ago. And they fixed the problem with PSQL.
So if you don’t have such an error when you connect to pqsl:
[postgres@stdb01 ~]$ psql
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”

You can skip the steps of initializing the DB. Create a DB, user, priv, and that’s all you need for that task.

Thank you so much… Its working as expected