When setting up PostgreSQL on a Linux system (particularly RHEL/CentOS using yum), you might encounter this error during initialization:
Data directory is not empty! [FAILED]
This typically happens when either:
- The data directory (/var/lib/pgsql/data) already contains files from a previous installation
- The PostgreSQL service was partially initialized before
- Another process created files in the directory
Here's the most straightforward fix:
sudo service postgresql stop
sudo rm -rf /var/lib/pgsql/data/*
sudo service postgresql initdb
sudo service postgresql start
If you want to preserve existing data (in case this isn't a fresh install), try:
sudo mv /var/lib/pgsql/data /var/lib/pgsql/data.old
sudo service postgresql initdb
sudo service postgresql start
Your pg_hba.conf modifications are correct for development environments, but note they reduce security by:
# WARNING: These settings disable password authentication for local connections
local all all trust
host all all 127.0.0.1/32 trust
For repeatable installations, consider this shell script:
#!/bin/bash
# PostgreSQL auto-installer for RHEL/CentOS
sudo yum install -y postgresql postgresql-server
sudo systemctl stop postgresql
sudo rm -rf /var/lib/pgsql/data
sudo postgresql-setup initdb
sudo sed -i 's/ident/trust/g' /var/lib/pgsql/data/pg_hba.conf
sudo systemctl start postgresql
sudo systemctl enable postgresql
If the error persists, check:
# Verify directory ownership
ls -ld /var/lib/pgsql/data
# Check for running processes
ps aux | grep postgres
# Verify SELinux context
ls -Z /var/lib/pgsql/data
For production systems, always:
- Maintain proper backups before modifying data directories
- Use md5 or scram-sha-256 authentication instead of trust
- Consider using separate data directories for major version upgrades
When setting up PostgreSQL on RHEL/CentOS systems using yum
, you might encounter this common initialization error. The system attempts to create a new database cluster but finds existing files in the designated data directory (/var/lib/pgsql/data
by default).
Several scenarios can trigger this:
1. Previous incomplete installation
2. Manual configuration files created before initdb
3. Package manager leaving residual files
4. Aborted previous initialization attempt
First, stop PostgreSQL if running:
sudo service postgresql stop
Then properly clean the data directory (make absolutely sure you're targeting the correct path):
sudo rm -rf /var/lib/pgsql/data/*
sudo rm -rf /var/lib/pgsql/data/.* # Removes hidden files too
After cleaning, follow this sequence:
sudo postgresql-setup initdb
sudo systemctl enable postgresql
sudo systemctl start postgresql
Check the initialization logs:
journalctl -u postgresql --no-pager -n 50
Validate directory ownership:
ls -ld /var/lib/pgsql/data/
# Should show postgres:postgres ownership
For more control, specify a new location:
sudo mkdir /pgdata
sudo chown postgres:postgres /pgdata
sudo -u postgres initdb -D /pgdata
After successful initdb, modify pg_hba.conf
as needed (your original modifications were correct):
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all 0.0.0.0/0 md5
Remember to reload after changes:
sudo systemctl reload postgresql
- Always check directory contents before initialization
- Consider using
pg_ctl
instead of service wrappers for more control - Document your PostgreSQL deployment steps