In PostgreSQL on Linux, it is crucial to understand that the OS user and the database user (often both named postgres) are two completely separate entities with separate credentials.
Here is the breakdown of how they interact:
1. Are the passwords the same?
No. * OS User (postgres): This is a Linux system account created during installation. By default, it usually has no password set (it is locked), meaning you cannot log in to it directly via SSH or su. You typically access it via sudo -u postgres.
- DB User (
postgres): This is a role inside the database. It has its own password stored in the PostgreSQL internal catalogs. - Recommendation: You do not need to set a password for the Linux
postgresuser. In fact, keeping it locked is more secure.
2. Can the OS user connect without a password?
Yes, by default.
On most Linux distributions (like Ubuntu, Debian, or CentOS), PostgreSQL is configured with a method called Peer Authentication for local connections.
- How it works: When you are logged in as the Linux user
postgresand run the commandpsql, the database “trusts” you because the kernel confirms your OS identity matches the requested database role. - The Command: This is why you can simply run:Bash
sudo -u postgres psqlYou will be logged in immediately without being asked for a database password.
3. Understanding pg_hba.conf
The “magic” behind this behavior is located in a file called pg_hba.conf (usually found in /etc/postgresql/[version]/main/).
| Connection Type | Database | User | Address | Method |
local | all | postgres | peer | |
local | all | all | peer | |
host | all | all | 127.0.0.1/32 | scram-sha-256 |
peer: Uses your Linux login name to authenticate. No password required if names match.scram-sha-256(ormd5): Requires a database password.1 This is typically used for “network” connections (even if the “network” is justlocalhost).
Summary Table
| User Type | Stored In | Primary Purpose | How to Access |
| Linux OS User | /etc/passwd | Owns the DB files & processes | sudo -i -u postgres |
| Database Role | pg_authid | Manages tables, data, & permissions | psql -U postgres |
To connect to the database user postgres remotely, you need to perform three main configuration steps. By default, PostgreSQL is “locked down” to only listen for local connections and often uses “Peer” authentication which doesn’t work over a network.
1. Set a Database Password1
Since remote connections cannot use your Linux OS identity (Peer authentication), you must set a password for the postgres database user.
- Log in locally:
sudo -u postgres psql - Run the password command:SQL
\password postgres - Enter your new secure password twice, then type
\qto exit.
2. Enable Remote Listening
By default, Postgres only listens on localhost. You need to tell it to listen on your network interface.
- Find your
postgresql.conf(usually in/etc/postgresql/[version]/main/). - Find the line:
#listen_addresses = 'localhost' - Change it to:Plaintext
listen_addresses = '*'(Note: Using'*'listens on all interfaces. For better security, you can use your specific server IP.)2
3. Update the Allow-list (pg_hba.conf)
You must explicitly tell Postgres which remote IPs are allowed to connect.3
- Open
pg_hba.confin the same directory. - Add a line at the bottom for your remote connection:Plaintext
# TYPE DATABASE USER ADDRESS METHOD host all postgres [YOUR_CLIENT_IP]/32 scram-sha-256host: Uses a TCP/IP connection.[YOUR_CLIENT_IP]/32: Replace this with your specific computer’s IP address. Using0.0.0.0/0allows everyone, which is a major security risk for thepostgressuperuser.
4. Restart and Test
After saving the files, restart the service to apply changes:
Bash
sudo systemctl restart postgresql
To connect from your remote machine, use:
Bash
psql -h [SERVER_IP] -U postgres
⚠️ Security Warning
Exposing the postgres superuser to the internet is dangerous.
- Best Practice: Use an SSH Tunnel instead. This lets you connect securely without opening ports or changing
pg_hba.conffor the whole world. - Firewall: Ensure your Linux firewall (like
ufw) allows traffic on port5432only from your specific IP:sudo ufw allow from [YOUR_IP] to any port 5432.
