How to write if statement for PostgreSQL DB status

Hi, Community.
I am trying to automate PostgreSQL software installation steps with Shell script. How can my script will output the “Green color or Red color” based on the below output. If status is Active it should output Green color " DB is started…" , If status is not active it should be output “Red color” DB is not started…" please help me how can i grep status from below output and incorportate in if statement of shell script. Thank you.

[cloud_user@postgres ~]$ sudo 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 Tue 2024-04-23 21:32:25 EDT; 1min 39s ago
Docs: PostgreSQL: Documentation: 12: PostgreSQL 12.18 Documentation
Main PID: 11251 (postmaster)
CGroup: /system.slice/postgresql-12.service
├─11251 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
├─11253 postgres: logger
├─11255 postgres: checkpointer
├─11256 postgres: background writer
├─11257 postgres: walwriter
├─11258 postgres: autovacuum launcher
├─11259 postgres: stats collector
└─11260 postgres: logical replication launcher

Apr 23 21:32:25 postgres systemd[1]: Starting PostgreSQL 12 database server…
Apr 23 21:32:25 postgres postmaster[11251]: 2024-04-23 21:32:25.244 EDT [11251] LOG: starting PostgreSQL 12.18 on x86_64-pc-linux-gnu, …, 64-bit
Apr 23 21:32:25 postgres postmaster[11251]: 2024-04-23 21:32:25.245 EDT [11251] LOG: listening on IPv6 address “::1”, port 5432
Apr 23 21:32:25 postgres postmaster[11251]: 2024-04-23 21:32:25.245 EDT [11251] LOG: listening on IPv4 address “127.0.0.1”, port 5432
Apr 23 21:32:25 postgres postmaster[11251]: 2024-04-23 21:32:25.248 EDT [11251] LOG: listening on Unix socket “/var/run/postgresql/.s.PGSQL.5432”
Apr 23 21:32:25 postgres postmaster[11251]: 2024-04-23 21:32:25.252 EDT [11251] LOG: listening on Unix socket “/tmp/.s.PGSQL.5432”
Apr 23 21:32:25 postgres postmaster[11251]: 2024-04-23 21:32:25.263 EDT [11251] LOG: redirecting log output to logging collector process
Apr 23 21:32:25 postgres postmaster[11251]: 2024-04-23 21:32:25.263 EDT [11251] HINT: Future log output will appear in directory “log”.
Apr 23 21:32:25 postgres systemd[1]: Started PostgreSQL 12 database server.
Hint: Some lines were ellipsized, use -l to show in full.

Here is a simple shell script to do as you ask:

#!/bin/bash

# Check the status of the PostgreSQL service
status=$(sudo systemctl status postgresql-12 | grep "Active:")

# Use a conditional statement to check if the status includes 'active (running)'
if [[ $status == *"active (running)"* ]]; then
    # Output in green colour
    echo -e "\e[32mDB is started...\e[0m"
else
    # Output in red colour
    echo -e "\e[31mDB is not started...\e[0m"
fi

Thank you that works.