Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Generate SSH without password authentication. Linux/Unix
  • remove archfiles only when it is applied to DR rm_archfiles.sh Linux/Unix
  • pvm_metric.sql for gathering report from vmstat tables Oracle
  • My Test Case On 21-OCT-2005 Oracle
  • Find Time Consuming SQL Statements in Oracle 10g Oracle
  • Backup and Recovery Scenarios Oracle
  • How to change hostname in Linux Linux/Unix
  • Oracle Internal Good Websites 1 Oracle
  • Nice notes on wait events Oracle
  • Remove DOS CR/LFs (^M) Linux/Unix
  • How to sort list of files on basis of their sizes. Linux/Unix
  • export import with parameter file. Oracle
  • Oracle 10g for solaris 10 Oracle
  • Does DBMS_JOB recompute the NEXT_DATE interval after or before Oracle
  • Important Script Method for tuning Oracle

Postgres DB user and OS user.

Posted on 25-Dec-202525-Dec-2025 By Admin No Comments on Postgres DB user and OS user.

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 postgres user. 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 postgres and run the command psql, 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:Bashsudo -u postgres psql You 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 TypeDatabaseUserAddressMethod
localallpostgrespeer
localallallpeer
hostallall127.0.0.1/32scram-sha-256
  • peer: Uses your Linux login name to authenticate. No password required if names match.
  • scram-sha-256 (or md5): Requires a database password.1 This is typically used for “network” connections (even if the “network” is just localhost).

Summary Table

User TypeStored InPrimary PurposeHow to Access
Linux OS User/etc/passwdOwns the DB files & processessudo -i -u postgres
Database Rolepg_authidManages tables, data, & permissionspsql -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.

  1. Log in locally: sudo -u postgres psql
  2. Run the password command:SQL\password postgres
  3. Enter your new secure password twice, then type \q to exit.

2. Enable Remote Listening

By default, Postgres only listens on localhost. You need to tell it to listen on your network interface.

  1. Find your postgresql.conf (usually in /etc/postgresql/[version]/main/).
  2. Find the line: #listen_addresses = 'localhost'
  3. Change it to:Plaintextlisten_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

  1. Open pg_hba.conf in the same directory.
  2. 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-256
    • host: Uses a TCP/IP connection.
    • [YOUR_CLIENT_IP]/32: Replace this with your specific computer’s IP address. Using 0.0.0.0/0 allows everyone, which is a major security risk for the postgres superuser.

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.conf for the whole world.
  • Firewall: Ensure your Linux firewall (like ufw) allows traffic on port 5432 only from your specific IP: sudo ufw allow from [YOUR_IP] to any port 5432.

POSTGRESQL

Post navigation

Previous Post: Trace a SQL session from another session using ORADEBUG
Next Post: Complete Git Tutorial for Beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (400)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (348)
  • SQL Server (6)
  • Uncategorized (3)
  • Videos (0)

Recent Posts

  • Running PDB on single node in RAC09-Apr-2026
  • find_arc.sql09-Apr-2026
  • pvm_pre_change.sql08-Apr-2026
  • find_encr_wallet.sql08-Apr-2026
  • find_pdbs.sql08-Apr-2026
  • Creating a Container Database using dbaascli08-Apr-2026
  • track_autoupgrade_copy_progress.sql01-Apr-2026
  • refre.sql for multitenant01-Apr-2026
  • prepfiles.sh for step by step generating pending statistics files10-Mar-2026
  • tracksqltime.sql05-Mar-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • VIvek Encryption Package and Its Usage Oracle
  • Finding locked objects Oracle
  • find the files that are 1 day old. Linux/Unix
  • CTAS with LONG Column for 9i and higher Oracle
  • scripts to take listener.log backup Linux/Unix
  • Backup and Recovery Scenarios Oracle
  • PLSQL Table Syntax 1 Oracle
  • oradebug ipcrm ipcs Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme