CentOS 7下PostgreSQL 9.6/9.5初始化及服务启动故障求助
Let's break down how to resolve the two key issues you're facing with PostgreSQL 9.5/9.6 on CentOS 7 for your Netbox setup:
Why postgresql96-setup is Missing
That initialization script is part of PostgreSQL's official RPM packages (hosted in their dedicated repository). If you installed PostgreSQL from CentOS's default base repo, this script won't exist—those packages use a different structure and don't include this helper tool.
Option 1: Install from PostgreSQL's Official Repo (to get the setup script)
If you want to use the postgresql96-setup tool, reinstall PostgreSQL from the official repo:
# Uninstall existing PostgreSQL packages first yum remove postgresql96-server postgresql96 # Add the official PostgreSQL 9.6 repo for CentOS 7 yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm # Install server and client packages yum install postgresql96-server postgresql96 # Now you can run the setup script as intended /usr/pgsql-9.6/bin/postgresql96-setup initdb
Option 2: Manually Initialize the Database (Keep Your Current Install)
Since you already have initdb in your /usr/pgsql-9.6/bin/ directory, you can initialize the database directly without the helper script:
- Verify the
postgressystem user exists (it should be created automatically during installation):id postgres # If missing, create it: useradd postgres - Switch to the
postgresuser (never run PostgreSQL as root—it's a security risk and causes permission issues):su - postgres - Initialize the database cluster, specifying the default data directory:
/usr/pgsql-9.6/bin/initdb -D /var/lib/pgsql/9.6/data - Fix directory permissions (critical for startup):
# Switch back to root exit chown -R postgres:postgres /var/lib/pgsql/9.6/data chmod 700 /var/lib/pgsql/9.6/data
Fixing the Failed PostgreSQL Service Startup
When systemctl shows the service failed, logs are your best friend—they'll tell you exactly what's wrong.
1. Check PostgreSQL's Local Logs
The default log directory for PostgreSQL 9.6 on CentOS 7 is /var/lib/pgsql/9.6/data/pg_log/. Look at the most recent log file for errors:
tail -n 50 /var/lib/pgsql/9.6/data/pg_log/*.log
Common issues you might see:
- Permission errors: The
postgresuser can't access the data directory (we fixed this above, but double-check) - Port conflict: Another service is using port 5432 (PostgreSQL's default). Verify with:
netstat -tulpn | grep 5432 - Configuration mistakes: Typos in
postgresql.conf(e.g., invalidlisten_addresses) or incorrect rules inpg_hba.conf
2. Use Systemd's Journal for Detailed Startup Logs
Run this command to get a granular breakdown of why the service failed:
journalctl -xeu postgresql-9.6.service
3. SELinux Troubleshooting
CentOS 7 has SELinux enabled by default, which can block PostgreSQL even if filesystem permissions are correct:
- Test if SELinux is the issue by temporarily disabling it (don't leave it disabled long-term):
setenforce 0 - If the service starts after disabling SELinux, apply the correct SELinux context to the data directory:
semanage fcontext -a -t postgresql_db_t "/var/lib/pgsql/9.6/data(/.*)?" restorecon -Rv /var/lib/pgsql/9.6/data # Re-enable SELinux setenforce 1
4. Start the Service Properly
Use systemctl (the recommended tool for CentOS 7) to manage the service:
systemctl start postgresql-9.6 # Enable it to start automatically on boot systemctl enable postgresql-9.6
内容的提问来源于stack exchange,提问作者uk_newt

