Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • move_arch_files.ksh Linux/Unix
  • Rman Notes -1 Oracle
  • DBMS_Shared_pool pinning triggers Oracle
  • import-export with multiple files Oracle
  • Drop database in Oracle 10g Oracle
  • Jai Shree Ram Oracle
  • For Perl DBI installation and testing program PHP/MYSQL/Wordpress
  • check_copy_progress.sh Linux/Unix
  • UNderstand and eliminate Latch contention. Oracle
  • PHP code to add WordPress posts in bulk programmatically PHP/MYSQL/Wordpress
  • RAC with RHEL4 and 11g Oracle
  • load SPM baseline from cursor cache Oracle
  • replace alphabets using sed Linux/Unix
  • move_arch_files.ksh Linux/Unix
  • Oracle10g – Using SQLAccess Advisor (DBMS_ADVISOR) with the Automatic Workload Repository 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 (402)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (350)
  • SQL Server (6)
  • Uncategorized (5)
  • Videos (0)

Recent Posts

  • SAT Mathematics 10 questions and answer at the end.30-Apr-2026
  • top 10 AI news today30-Apr-2026
  • runon_allpdbs_show_conname.sh23-Apr-2026
  • runon_allcdbs_find_pdbs.sql23-Apr-2026
  • 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

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • How to change hostname in Linux Linux/Unix
  • SQL_PROFILE – I explaination Oracle
  • Does DBMS_JOB recompute the NEXT_DATE interval after or before Oracle
  • Implementing Listener Security Oracle
  • tblwopk.sql tablewopk.sql Oracle
  • Facts about SCN and Rollback Segment Oracle
  • ORA-8031 issue and solution if it is occuring due to truncate. Oracle
  • The most important Tuning Notes Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme