Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • 272332.1 CRS 10g Diagnostic Collection Guide Oracle
  • avail.sh ( find filesystem spae usage) Linux/Unix
  • chk_space_SID.ksh Linux/Unix
  • RAC 11g with vmware Oracle
  • To Find Orphan OS processes. Linux/Unix
  • How to stop OCSSD Daemon Oracle
  • Adding a new disk and mount it automatically. on VMWARE LINUX Linux/Unix
  • Read CSV file using PySpark Python/PySpark
  • TOP-N Sql to find Nth max or Top N rows Oracle
  • longtx.sql with the flag whether session is blocking any DML locks or not. Oracle
  • Another export with Query Oracle
  • Temporary Tablespsace Temp tablespace behaviour Oracle
  • Generating XML from SQLPLUS Oracle
  • find_longsql.sql Oracle
  • Reading parameter file and printing Linux/Unix

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 (395)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (343)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • prepfiles.sh for step by step generating pending statistics files10-Mar-2026
  • tracksqltime.sql05-Mar-2026
  • Complete Git Tutorial for Beginners25-Dec-2025
  • Postgres DB user and OS user.25-Dec-2025
  • Trace a SQL session from another session using ORADEBUG30-Sep-2025
  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • adding new line after specific pattern using sed Linux/Unix
  • find_du.ksh to find # of files, their sizes in current folder and its subdolder Linux/Unix
  • Test Case for Inserting Multiple (2.3 Million rows in 26 Seconds) Oracle
  • findobj.sql Oracle
  • CPU speed on Linux Linux/Unix
  • Renaming the column name Oracle
  • oracle_env_10g_CADEV Linux/Unix
  • SQL_PLAN.sql for checking real execution plan Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme