Hello! Welcome back to your course on Linux system administration for web developers.
In our last lesson, you mastered the critical skill of backing up and restoring your PostgreSQL database with pg_dump and pg_restore. This protects your data from loss. Today, we'll address a different kind of protection: ensuring your database can run correctly within Fedora's powerful security framework, SELinux.
Our goal for this lesson is to learn how to troubleshoot and apply necessary SELinux contexts or booleans for PostgreSQL operation. You've encountered SELinux before with Nginx, and now we'll apply those concepts specifically to your database server. By the end of this lesson, you'll know how to diagnose and fix the common SELinux issues that can prevent PostgreSQL from working correctly, a crucial skill for deploying applications on Fedora.
1. A Quick SELinux Refresher
Remember from our earlier lessons that SELinux (Security-Enhanced Linux) adds a layer of security on top of standard Linux file permissions. While file permissions control which users can access files, SELinux controls which processes can access files, ports, and other processes.

Every process and file on your system has an SELinux label, called a context. SELinux enforces a policy that defines how contexts can interact. For example, a process running with the httpd_t (web server) context is, by default, not allowed to access files labeled postgresql_data_t (PostgreSQL data files). This is a good thing! It means a compromised web server can't directly tamper with your database files.
However, this also means that misconfigurations can cause SELinux to block legitimate operations. The most common issues you'll face with PostgreSQL are:
- Incorrect File Contexts: Files have the wrong SELinux label, often after being moved or restored from a backup.
- Policy Restrictions: The default policy prevents a necessary action, like your Django web application connecting to the database over the network.
2. Is SELinux the Problem? Finding the Evidence
Your first step in troubleshooting is to determine if SELinux is the cause of an issue. A classic sign is a "Permission denied" error when standard file permissions (ls -l) look correct.
When SELinux blocks an action, it logs an AVC (Access Vector Cache) denial. You can find these logs using a few key commands.
How to Install PostgreSQL 14 on Fedora - Configure SELinux for PostgreSQL
This article from LinuxCapable has a section dedicated to SELinux and PostgreSQL on Fedora. It provides the exact commands you'll need to check for denials.
Please read the section titled 'Configure SELinux for PostgreSQL'. Focus on the subsections 'Check SELinux Status' and 'Troubleshoot SELinux Denials' to see the commands used to check logs.
As the article shows, the primary tool for this is ausearch. To find recent denials related to PostgreSQL, you would run:
sudo ausearch -m avc -ts recent | grep postgres
-m avc: Searches for AVC (denial) messages.-ts recent: Limits the search to the near past (e.g., the last 10 minutes).| grep postgres: Filters the output to show only messages containing "postgres".
If this command returns any output, SELinux has recently blocked PostgreSQL from doing something. Another helpful tool you've seen before is sealert, which can give you human-readable reports and suggestions.
3. Hands-On: Fixing File Contexts
One of the most common times you'll encounter an SELinux issue is after restoring a database from a backup, just like we did in the last lesson. If you move or copy files, they might end up with the wrong SELinux context, and PostgreSQL will fail to start.
Let's simulate this scenario and fix it.
Your Task: A File Context Fire Drill
-
Check the Correct Context: First, let's see what the correct context for PostgreSQL configuration files should be.
# The `-Z` flag shows SELinux contexts ls -lZ /var/lib/pgsql/data/postgresql.confYou should see a context ending in
postgresql_conf_t. -
Simulate the "Bad Restore": Now, let's intentionally change the context of the main configuration file to something incorrect, like the context for files in a user's home directory.
# Stop postgresql first sudo systemctl stop postgresql # Use 'chcon' to change the context temporarily sudo chcon -t user_home_t /var/lib/pgsql/data/postgresql.conf # Verify the change ls -lZ /var/lib/pgsql/data/postgresql.confThe context should now end in
user_home_t. -
Trigger the Failure: Try to start PostgreSQL.
sudo systemctl start postgresqlIt will fail. To see why, check the status:
systemctl status postgresqlThe output will likely show a generic failure. Now, let's look for the real cause.
-
Find the Denial: Use
ausearchto find the SELinux denial message.sudo ausearch -m avc -ts recent | grep postgresYou should see a log entry indicating that the
postgresprocess was deniedreadaccess topostgresql.confbecause of a type mismatch. -
Fix the Context: The best way to fix incorrect contexts is with
restorecon. This command reads the default SELinux policy rules and restores the correct contexts for files and directories.# -R is for recursive, -v is for verbose (to see what it changes) sudo restorecon -Rv /var/lib/pgsql/data/You should see output indicating that
postgresql.confwas relabeled fromuser_home_tback topostgresql_conf_t. -
Verify the Fix: Start the PostgreSQL service again.
sudo systemctl start postgresql sudo systemctl status postgresqlIt should now start successfully!
This exercise demonstrates the fundamental workflow for fixing label-related SELinux issues: identify the problem with ausearch, and fix it with restorecon.
4. Hands-On: Allowing Network Connections with Booleans
The second major issue you'll face is allowing your web application to communicate with your database. In our deployment stack (Nginx -> Gunicorn -> Django), the Gunicorn process is often run with the httpd_t context. By default, SELinux policy does not allow the httpd_t context to make network connections to a database.
This is where SELinux booleans come in. They are simple on/off switches that allow you to tweak parts of the security policy without writing custom rules.
How to Optimize SELinux on Linux Server for Better Security
The following article gives a great, practical overview of the most important booleans, including the exact one we need for our Django application.
Read the sections 'Tune With SELinux Booleans' and the practical example 'Django App Talking To PostgreSQL On Non-Default Port'. Focus on the boolean httpd_can_network_connect_db and the setsebool command.
Your Task: Open the Port for Your Web App
-
Check the Boolean: Let's see if your system currently allows web servers to connect to databases.
getsebool httpd_can_network_connect_dbThe output will likely be
httpd_can_network_connect_db --> off. -
Enable the Boolean: To allow your Django application to connect to PostgreSQL, you need to turn this boolean on.
# The -P flag makes the change persistent across reboots. sudo setsebool -P httpd_can_network_connect_db onThe
-Pis important; without it, the setting would reset the next time you restart your server. -
Verify the Change: Check the boolean's value again.
getsebool httpd_can_network_connect_dbIt should now be
httpd_can_network_connect_db --> on.
With this single command, you have adjusted the SELinux policy to permit one of the most common and necessary operations for a web stack.
Conclusion
Great job! You've successfully navigated the intersection of PostgreSQL and SELinux. While SELinux can seem daunting at first, you've now learned a repeatable, logical process for diagnosing and resolving the most common issues a web developer will face. This knowledge is invaluable for creating a secure and functional production environment on Fedora or any other RHEL-based distribution.
Key Takeaways:
- Incorrect file contexts are a common problem after moving files or restoring backups. Use
ls -Zto check contexts andrestoreconto fix them based on the system's default policy. - SELinux booleans are on/off switches for policy rules. Use
getseboolto check them andsetsebool -Pto change them permanently. - The
httpd_can_network_connect_dbboolean is essential for allowing your web application (running under anhttpd_tcontext) to connect to your PostgreSQL database. - Always check the audit logs with
ausearch -m avc -ts recentwhen you suspect an SELinux issue.
Preview of the Next Lesson:
Your database is now configured, secured with user roles, protected by SELinux, and you know how to back it up. The final step is to ensure those backups happen regularly without manual intervention. In our next lesson, we will learn how to automate periodic database backups using a systemd timer, a modern and powerful way to schedule tasks on your Fedora system.