It’s a common error faced and can be resolved with some simple steps
Run the following command for more information about the status of the service:
sudo systemctl status postgresql.service
Look for error messages on why it might be failing.
View PostgreSQL logs for any error messages:
sudo journalctl -u postgresql.service
This will provide more detailed logs of what went wrong during booting up.
Check if PostgreSQL configuration files are set correctly:
Check the postgresql.conf and pg_hba.conf file for misconfiguration. Should be in /etc/postgresql/{version}/main/or /var/lib/pgsql/{version}/data/.
Do not have another application or service using the port, which PostgreSQL is trying to work on. If PostgreSQL operates on 5432 default, the following should bring no output:
sudo netstat -tuln | grep 5432
Make sure, that the ownership and that the permission of the directory of PostgreSQL data is correctly set:
sudo chown -R postgres:postgres /var/lib/postgresql/{version}/main/
Replace {version} with your version of PostgreSQL.
6. Initializing the Database Cluster
If this is the first PostgreSQL start, an initialization of the database cluster may also be required as follows (note that on some CentOS 7 installation, instead of /var/lib/postgres/data use /usr/local/pgsql/data with appropriate permissions in /usr/bin/postgres/postgres.conf:
sudo -u postgres /usr/pgsql-{version}/bin/initdb -D /var/lib/pgsql/{version}/data/
Once above checks have been addressed start the service again as:
sudo systemctl start postgresql.service
8. Activate PostgreSQL to boot
Add this line at the very end to get PostgreSQL auto-started
sudo systemctl enable postgresql.service
9. Test SELinux / App Armor
If you use SELinux or AppArmor on your system, check the logs and configuration-they might be blocking the start of PostgreSQL.
If everything fails, reboot your server and try to start the service again.