使用EnterpriseDB安装PostgreSQL12时未创建postgres用户求助
Let’s work through resolving this issue where the postgres superuser failed to create during your EnterpriseDB PostgreSQL 12 setup, causing the FATAL: password authentication failed for user "postgres" error when running sudo -u postgres psql.
Step 1: Check if the System postgres User Exists
First, confirm whether the system-level postgres user was actually created:
id postgres
If you get an error like id: postgres: no such user, we’ll need to create this user first.
Step 2: Create the System postgres User
Run these commands to create the user and set a password (note: the system user’s password isn’t strictly required for PostgreSQL access, but setting it helps with consistency):
sudo useradd -m postgres sudo passwd postgres
The -m flag ensures a home directory is created for the user, which some PostgreSQL setups rely on.
Step 3: Initialize the PostgreSQL Database Cluster
Since the postgres user was missing, it’s likely the database cluster wasn’t initialized. Locate the initdb binary from your EnterpriseDB installation (usually in /usr/edb/as12/bin/ for PostgreSQL 12):
sudo -u postgres /usr/edb/as12/bin/initdb -D /var/lib/edb/as12/data
Adjust the paths if your EnterpriseDB installation uses a different directory structure (check your install logs if you’re unsure).
Step 4: Fix Data Directory Permissions
Make sure the PostgreSQL data directory is owned by the postgres user to avoid permission-related errors:
sudo chown -R postgres:postgres /var/lib/edb/as12/data
Step 5: Start and Enable the PostgreSQL Service
EnterpriseDB uses a service name like edb-as-12 for PostgreSQL 12. Start the service and set it to run on boot:
sudo systemctl start edb-as-12 sudo systemctl enable edb-as-12
Step 6: Set the Database postgres User Password
Now, set a password for the database-level postgres superuser using the EnterpriseDB psql binary:
sudo -u postgres /usr/edb/as12/bin/psql -c "ALTER USER postgres PASSWORD 'your_secure_password';"
Replace your_secure_password with a strong password of your choice.
Step 7: Adjust Authentication Settings (pg_hba.conf)
Check the pg_hba.conf file (located in /var/lib/edb/as12/data/) to ensure local connections are allowed. Look for lines like this:
# Local connections local all postgres peer
If you prefer password authentication over peer (which uses system user credentials), change it to:
local all postgres md5
After making changes, restart the service to apply them:
sudo systemctl restart edb-as-12
Step 8: Test the Connection
Try connecting again with:
sudo -u postgres psql
Or if you set up password authentication:
psql -U postgres -h localhost
You should now be able to access the PostgreSQL shell without errors.
内容的提问来源于stack exchange,提问作者Marat Mag

