Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

10.4. Configuration Examples


10.4.1. PostgreSQL Changing Database Location

When using Red Hat Enterprise Linux 6, the default location for PostgreSQL to store its database is /var/lib/pgsql/data. This is where SELinux expects it to be by default, and hence this area is already labeled appropriately for you, using the postgresql_db_t type.
The area where the database is located can be changed depending on individual environment requirements or preferences, however it is important that SELinux is aware of this new location; that it is labeled accordingly. This example explains how to change the location of a PostgreSQL database and then how to label the new location so that SELinux can still provide its protection mechanisms to the new area based on its contents.
Note that this is an example only and demonstrates how SELinux can affect PostgreSQL. Comprehensive documentation of PostgreSQL is beyond the scope of this document. Refer to the official PostgreSQL documentation for further details. This example assumes that the postgresql-server package is installed.
  1. Run the ls -lZ /var/lib/pgsql command to view the SELinux context of the default database location for postgresql:
    Copy to Clipboard Toggle word wrap
    ~]# ls -lZ /var/lib/pgsql
    drwx------. postgres postgres system_u:object_r:postgresql_db_t:s0 data
    
    This shows postgresql_db_t which is the default context element for the location of database files. This context will have to be manually applied to the new database location that will be used in this example in order for it to function properly.
  2. Create a new directory for the new location of the database(s). In this example, /opt/postgresql/data/ is used. If you use a different location, replace the text in the following steps with your location:
    Copy to Clipboard Toggle word wrap
    ~]# mkdir -p /opt/postgresql/data
  3. Perform a directory listing of the new location. Note that the initial context of the new directory is usr_t. This context is not sufficient for SELinux to offer its protection mechanisms to PostgreSQL. Once the context has been changed, it will be able to function properly in the new area.
    Copy to Clipboard Toggle word wrap
    ~]# ls -lZ /opt/postgresql/
    drwxr-xr-x. root root unconfined_u:object_r:usr_t:s0   data
    
  4. Change the ownership of the new location to allow access by the postgres user and group. This sets the traditional Unix permissions which SELinux will still observe.
    Copy to Clipboard Toggle word wrap
    ~]# chown -R postgres:postgres /opt/postgresql
  5. Open the PostgreSQL init file /etc/rc.d/init.d/postgresql with a text editor and modify the PGDATA and PGLOG variables to point to the new location:
    Copy to Clipboard Toggle word wrap
    ~]# vi /etc/rc.d/init.d/postgresql
    PGDATA=/opt/postgresql/data
    PGLOG=/opt/postgresql/data/pgstartup.log
    
    Save this file and exit the text editor.
  6. Initialize the database in the new location.
    Copy to Clipboard Toggle word wrap
    ~]$ su - postgres -c "initdb -D /opt/postgresql/data"
  7. Having changed the database location, starting the service will fail at this point:
    Copy to Clipboard Toggle word wrap
    ~]# service postgresql start
    Starting postgresql service: 			[FAILED]
    
    SELinux has caused the service to not start. This is because the new location is not properly labelled. The following steps explain how to label the new location (/opt/postgresql/) and start the postgresql service properly:
  8. Run the semanage command to add a context mapping for /opt/postgresql/ and any other directories/files within it:
    Copy to Clipboard Toggle word wrap
    ~]# semanage fcontext -a -t postgresql_db_t "/opt/postgresql(/.*)?"
  9. This mapping is written to the /etc/selinux/targeted/contexts/files/file_contexts.local file:
    Copy to Clipboard Toggle word wrap
    ~]# grep -i postgresql /etc/selinux/targeted/contexts/files/file_contexts.local
    
    /opt/postgresql(/.*)?    system_u:object_r:postgresql_db_t:s0
    
  10. Now use the restorecon command to apply this context mapping to the running system:
    Copy to Clipboard Toggle word wrap
    ~]# restorecon -R -v /opt/postgresql
  11. Now that the /opt/postgresql/ location has been labeled with the correct context for PostgreSQL, the postgresql service will start successfully:
    Copy to Clipboard Toggle word wrap
    ~]# service postgresql start
    Starting postgreSQL service:                                            [  OK  ]
    
  12. Confirm the context is correct for /opt/postgresql/:
    Copy to Clipboard Toggle word wrap
    ~]$ ls -lZ /opt
    drwxr-xr-x. root root system_u:object_r:postgresql_db_t:s0 postgresql
    
  13. Check with the ps command that the postgresql process displays the new location:
    Copy to Clipboard Toggle word wrap
    ~]# ps aux | grep -i postmaster
    
    postgres 21564  0.3  0.3  42308  4032 ?        S    10:13   0:00 /usr/bin/postmaster -p 5432 -D /opt/postgresql/data/
  14. The location has been changed and labeled, and the postgresql daemon has started successfully. At this point all running services should be tested to confirm normal operation.
Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat, Inc.