{"id":107,"date":"2026-07-17T00:00:00","date_gmt":"2026-07-17T00:00:00","guid":{"rendered":"http:\/\/\/var\/www\/html\/?p=107"},"modified":"2026-07-19T16:42:02","modified_gmt":"2026-07-19T16:42:02","slug":"postgresql-pghbaconf-connection-authorization-failed-on-centos-stream-rocky-linux","status":"publish","type":"post","link":"https:\/\/staging.butitworkedlocal.com\/?p=107","title":{"rendered":"PostgreSQL pg_hba.conf Connection Authorization Failed on CentOS Stream \/ Rocky Linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Troubleshoot and resolve &#039;connection authorization failed&#039; errors in PostgreSQL on CentOS Stream and Rocky Linux by correctly configuring pg_hba.conf and related settings.<\/strong><\/p>\n\n\n<p>When working with PostgreSQL on CentOS Stream or Rocky Linux, encountering a &quot;connection authorization failed&quot; error indicates that the database server successfully received your connection request but explicitly denied it based on its access control rules. This guide provides a comprehensive, expert-level approach to diagnose and resolve this common issue, ensuring your applications and users can connect securely.<\/p>\n\n<h3>Symptom &amp; Error Signature<\/h3>\n\n<p>The primary symptom is an inability to connect to your PostgreSQL database, typically from a client application, a command-line <code>psql<\/code> utility, or another server. You will usually see a <code>FATAL<\/code> error message.<\/p>\n\n<p><strong>Typical <code>psql<\/code> command line error:<\/strong><\/p>\n\n<pre><code class=\"language-bash\">$ psql -h your_db_host -U your_db_user -d your_db_name\npsql: FATAL:  connection authorization failed for user &quot;your_db_user&quot;\npsql: FATAL:  no pg_hba.conf entry for host &quot;your_client_ip&quot;, user &quot;your_db_user&quot;, database &quot;your_db_name&quot;, no encryption<\/code><\/pre>\n\n<p><strong>Common application error (e.g., Python with psycopg2):<\/strong><\/p>\n\n<pre><code class=\"language-python\"># Example output from a Python application attempting to connect\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\npsycopg2.OperationalError: FATAL:  connection authorization failed for user &quot;web_app_user&quot;\nFATAL:  no pg_hba.conf entry for host &quot;192.168.1.100&quot;, user &quot;web_app_user&quot;, database &quot;webapp_db&quot;<\/code><\/pre>\n\n<p><strong>PostgreSQL server log entries (found in <code>\/var\/lib\/pgsql\/data\/log\/postgresql-*.log<\/code> or <code>journalctl -u postgresql-1X<\/code>):<\/strong><\/p>\n\n<pre><code class=\"language-\">202X-XX-XX XX:XX:XX UTC [12345] LOG:  connection received: host=192.168.1.100 port=54321\n202X-XX-XX XX:XX:XX UTC [12345] FATAL:  no pg_hba.conf entry for host &quot;192.168.1.100&quot;, user &quot;web_app_user&quot;, database &quot;webapp_db&quot;, no encryption\n```<\/code><\/pre>\n\n<h3>Root Cause Analysis<\/h3>\n\n<p>The &quot;connection authorization failed&quot; error almost exclusively points to an incorrect or missing entry in PostgreSQL&#039;s Host-Based Authentication (HBA) configuration file, <code>pg_hba.conf<\/code>. This file controls which hosts are allowed to connect, which users they can connect as, which databases they can access, and what authentication method is required.<\/p>\n\n<p>The underlying reasons typically fall into one of these categories:<\/p>\n\n<ol><li> <strong>Missing <code>pg<em>hba.conf<\/code> Entry:<\/strong> The most common cause. There is no rule in <code>pg<\/em>hba.conf<\/code> that matches the incoming connection&#039;s parameters (source IP, user, database).<\/li><li> <strong>Incorrect <code>pg_hba.conf<\/code> Entry:<\/strong> An existing entry is present, but one or more of its fields (e.g., source IP, user, database, authentication method) do not precisely match the connection attempt.<\/li><li> <strong>Incorrect Order of Rules:<\/strong> <code>pg_hba.conf<\/code> rules are processed sequentially from top to bottom. The <em>first<\/em> rule that matches the connection attempt is used. If a broad, less secure rule appears before a more specific, secure rule, it might inadvertently allow or deny connections in unexpected ways.<\/li><li> <strong>Incorrect Authentication Method:<\/strong> The <code>pg_hba.conf<\/code> entry specifies an authentication method (e.g., <code>scram-sha-256<\/code>, <code>md5<\/code>, <code>trust<\/code>, <code>peer<\/code>, <code>ident<\/code>) that doesn&#039;t match the client&#039;s provided credentials or the server&#039;s configured user password.<\/li><li>    *   <code>scram-sha-256<\/code>: The modern, recommended secure password-based authentication.<\/li><li>    *   <code>md5<\/code>: An older, less secure password-based authentication, still widely used.<\/li><li>    *   <code>trust<\/code>: Allows anyone to connect without a password (highly insecure for non-local connections).<\/li><li>    *   <code>peer<\/code>: Used for local connections where the operating system user matches the database user.<\/li><li>    *   <code>ident<\/code>: Similar to <code>peer<\/code>, relies on an ident server on the client for authentication.<\/li><li> <strong><code>listen<em>addresses<\/code> Misconfiguration:<\/strong> While this usually results in &quot;connection refused,&quot; if <code>listen<\/em>addresses<\/code> in <code>postgresql.conf<\/code> is set to <code>localhost<\/code> or <code>127.0.0.1<\/code> and a remote client tries to connect, the connection will not even reach the <code>pg_hba.conf<\/code> stage for remote IP addresses. It&#039;s essential to ensure PostgreSQL is listening on the correct network interfaces (e.g., <code>*<\/code> for all, or specific IP addresses).<\/li><li> <strong>Incorrect Database\/User Permissions:<\/strong> Even if <code>pg<em>hba.conf<\/code> allows the connection, the user might not have <code>CONNECT<\/code> privileges on the requested database or <code>USAGE<\/code> on specific schemas, leading to application errors after authentication. This is different from the <code>pg<\/em>hba.conf<\/code> error but often confused.<\/li><\/ol>\n\n<h3>Step-by-Step Resolution<\/h3>\n\n<p>Follow these steps carefully to diagnose and resolve the <code>pg_hba.conf<\/code> connection authorization error.<\/p>\n\n<h4>1. Locate <code>pg_hba.conf<\/code> and <code>postgresql.conf<\/code><\/h4>\n\n<p>First, you need to find the correct configuration files. The location can vary slightly depending on the PostgreSQL version and installation method.<\/p>\n\n<pre><code class=\"language-bash\"># Log in as the postgres user (or use sudo) to execute psql commands\nsudo -u postgres psql -c &#039;SHOW hba_file;&#039;\nsudo -u postgres psql -c &#039;SHOW config_file;&#039;<\/code><\/pre>\n\n<p><strong>Common locations on CentOS Stream \/ Rocky Linux for PostgreSQL 12-16:<\/strong><\/p>\n\n<ul><li>  <strong><code>pg<em>hba.conf<\/code><\/strong>: <code>\/var\/lib\/pgsql\/data\/pg<\/em>hba.conf<\/code> (for older versions\/manual setup) or <code>\/var\/lib\/pgsql\/1X\/data\/pg_hba.conf<\/code> (where <code>1X<\/code> is your PostgreSQL major version, e.g., <code>15<\/code>).<\/li><li>  <strong><code>postgresql.conf<\/code><\/strong>: <code>\/var\/lib\/pgsql\/data\/postgresql.conf<\/code> or <code>\/var\/lib\/pgsql\/1X\/data\/postgresql.conf<\/code>.<\/li><\/ul>\n\n<blockquote><p>[!NOTE]\nOn modern CentOS\/Rocky systems, PostgreSQL is often installed via <code>dnf<\/code>, and the <code>data<\/code> directory is version-specific (e.g., <code>\/var\/lib\/pgsql\/15\/data<\/code>).<\/p><\/blockquote>\n\n<h4>2. Backup Original Configuration Files<\/h4>\n\n<p>Before making any changes, always back up your configuration files.<\/p>\n\n<pre><code class=\"language-bash\">PG_VERSION=$(sudo -u postgres psql -t -P format=unaligned -c &#039;SHOW hba_file;&#039; | cut -d&#039;\/&#039; -f5) # Extracts &#039;15&#039; from &#039;\/var\/lib\/pgsql\/15\/data\/pg_hba.conf&#039;<\/code><\/pre>\n\n<p>sudo cp ${PG<em>CONFIG<\/em>DIR}\/pg<em>hba.conf ${PG<\/em>CONFIG<em>DIR}\/pg<\/em>hba.conf.bak.$(date +%F-%H%M)\nsudo cp ${PG<em>CONFIG<\/em>DIR}\/postgresql.conf ${PG<em>CONFIG<\/em>DIR}\/postgresql.conf.bak.$(date +%F-%H%M)\n<code><\/code>`<\/p>\n\n<h4>3. Understand <code>pg_hba.conf<\/code> Syntax<\/h4>\n\n<p>Each line in <code>pg_hba.conf<\/code> defines an access rule. Comments start with <code>#<\/code>. Blank lines are ignored.\nA rule typically follows this format:<\/p>\n\n<p><code>TYPE  DATABASE  USER  ADDRESS  METHOD  [OPTIONS]<\/code><\/p>\n\n<ul><li>  <strong><code>TYPE<\/code><\/strong>: Specifies the connection type.<\/li><li>  *   <code>local<\/code>: Connections via Unix-domain sockets (local access only).<\/li><li>  *   <code>host<\/code>: Connections via TCP\/IP (both IPv4 and IPv6).<\/li><li>  *   <code>hostssl<\/code>: TCP\/IP connections only if SSL is used.<\/li><li>  <em>   <code>hostnossl<\/code>: TCP\/IP connections only if SSL is <\/em>not* used.<\/li><li>  <strong><code>DATABASE<\/code><\/strong>: Which database(s) this rule applies to. Can be <code>all<\/code>, a specific database name, or <code>replication<\/code> (for streaming replication).<\/li><li>  <strong><code>USER<\/code><\/strong>: Which user(s) this rule applies to. Can be <code>all<\/code>, a specific user name, or a group name prefixed with <code>+<\/code>.<\/li><li>  <strong><code>ADDRESS<\/code><\/strong>: The client&#039;s IP address range or host.<\/li><li>  *   <code>127.0.0.1\/32<\/code> or <code>localhost<\/code>: Only from the local machine (IPv4).<\/li><li>  *   <code>::1\/128<\/code>: Only from the local machine (IPv6).<\/li><li>  *   <code>0.0.0.0\/0<\/code>: All IPv4 addresses (highly insecure for most authentication methods).<\/li><li>  *   <code>192.168.1.0\/24<\/code>: A specific network range.<\/li><li>  *   <code>10.0.0.10\/32<\/code>: A single specific IP address.<\/li><li>  <strong><code>METHOD<\/code><\/strong>: The authentication method. <code>scram-sha-256<\/code> (recommended), <code>md5<\/code>, <code>trust<\/code>, <code>peer<\/code>, <code>ident<\/code>, <code>gssapi<\/code>, <code>ssi<\/code>.<\/li><li>  <strong><code>OPTIONS<\/code><\/strong>: Additional options specific to the authentication method.<\/li><\/ul>\n\n<h4>4. Edit <code>pg_hba.conf<\/code> to Allow Connections<\/h4>\n\n<p>Using the information from the error message (client IP, user, database), add or modify an entry in <code>pg_hba.conf<\/code>. Open the file with your preferred text editor (e.g., <code>vi<\/code> or <code>nano<\/code>).<\/p>\n\n<pre><code class=\"language-bash\">sudo vi ${PG_CONFIG_DIR}\/pg_hba.conf<\/code><\/pre>\n\n<p><strong>Common Scenarios and Solutions:<\/strong><\/p>\n\n<p><strong>Scenario 1: Allow a specific application user from a specific IP address (most common and recommended).<\/strong>\nAdd this line at the <em>end<\/em> of your <code>pg_hba.conf<\/code> file, or logically group it with other <code>host<\/code> entries:<\/p>\n\n<pre><code class=\"language-ini\"># TYPE  DATABASE        USER            ADDRESS                 METHOD\nhost    webapp_db       web_app_user    192.168.1.100\/32        scram-sha-256\n```\n*   Replace `webapp_db` with your database name.\n*   Replace `web_app_user` with your database username.\n*   Replace `192.168.1.100\/32` with the *exact IP address* of the client connecting to PostgreSQL. Use `\/32` for a single IPv4 address or `\/128` for a single IPv6 address. For a network, use the appropriate CIDR (e.g., `192.168.1.0\/24`).<\/code><\/pre>\n\n<p><strong>Scenario 2: Allow all users from <code>localhost<\/code> for a specific database (for local applications\/CLI tools).<\/strong><\/p>\n\n<pre><code class=\"language-ini\"># TYPE  DATABASE        USER            ADDRESS                 METHOD\nhost    your_db_name    all             127.0.0.1\/32            scram-sha-256\nhost    your_db_name    all             ::1\/128                 scram-sha-256<\/code><\/pre>\n\n<p><strong>Scenario 3: Allow local connections using <code>peer<\/code> authentication (recommended for local <code>postgres<\/code> user).<\/strong>\nThis is typically already present and allows the Linux <code>postgres<\/code> user to connect to PostgreSQL as the <code>postgres<\/code> database user via Unix sockets without a password.<\/p>\n\n<pre><code class=\"language-ini\"># TYPE  DATABASE        USER            ADDRESS                 METHOD\nlocal   all             postgres                                peer<\/code><\/pre>\n\n<blockquote><p>[!WARNING]\nAvoid using <code>trust<\/code> for remote connections (<code>host<\/code>) as it allows anyone to connect without any authentication. Only use <code>trust<\/code> for <code>local<\/code> connections in highly controlled environments or for specific, temporary debugging.<\/p>\n\n<p><code>host    all             all             0.0.0.0\/0               md5<\/code> &#8211; This rule is highly insecure as it allows all users from any IP to connect to any database using a password. Only use <code>0.0.0.0\/0<\/code> if you have very strict firewall rules in place, and even then, consider restricting it.<\/p><\/blockquote>\n\n<h4>5. Verify <code>listen_addresses<\/code> in <code>postgresql.conf<\/code><\/h4>\n\n<p>While <code>pg_hba.conf<\/code> handles <em>authorization<\/em>, <code>postgresql.conf<\/code> determines <em>where<\/em> PostgreSQL listens for connections. If PostgreSQL isn&#039;t listening on the correct network interface, remote connections will result in &quot;connection refused,&quot; not &quot;authorization failed.&quot; However, it&#039;s a common point of confusion.<\/p>\n\n<p>Open <code>postgresql.conf<\/code>:<\/p>\n\n<pre><code class=\"language-bash\">sudo vi ${PG_CONFIG_DIR}\/postgresql.conf<\/code><\/pre>\n\n<p>Find the <code>listen_addresses<\/code> parameter and ensure it&#039;s configured correctly:<\/p>\n\n<pre><code class=\"language-ini\"># What IP address(es) to listen on; &#039;*&#039; means all IP interfaces.\n# In a production environment, it is best to be explicit.\n#listen_addresses = &#039;localhost&#039;         # (change requires restart)\nlisten_addresses = &#039;*&#039;                  # Listen on all available interfaces\n#listen_addresses = &#039;192.168.1.50,localhost&#039; # Listen on specific IPs and localhost<\/code><\/pre>\n\n<blockquote><p>[!IMPORTANT]\nChanging <code>listen_addresses<\/code> requires a <strong>restart<\/strong> of the PostgreSQL service, not just a reload.<\/p><\/blockquote>\n\n<h4>6. Reload or Restart PostgreSQL<\/h4>\n\n<p>After modifying <code>pg<em>hba.conf<\/code>, you must reload PostgreSQL for the changes to take effect. If you changed <code>listen<\/em>addresses<\/code> in <code>postgresql.conf<\/code>, a full restart is required.<\/p>\n\n<p><strong>Reload (for <code>pg_hba.conf<\/code> changes):<\/strong><\/p>\n\n<pre><code class=\"language-bash\"># Get the PostgreSQL service name (e.g., postgresql-15)<\/code><\/pre>\n\n<p>sudo systemctl reload ${PG_SERVICE}\n<code><\/code>`<\/p>\n\n<p><strong>Restart (for <code>postgresql.conf<\/code> changes or if reload doesn&#039;t work):<\/strong><\/p>\n\n<pre><code class=\"language-bash\">sudo systemctl restart ${PG_SERVICE}<\/code><\/pre>\n\n<blockquote><p>[!NOTE]\n<code>systemctl reload<\/code> is generally preferred as it doesn&#039;t drop existing connections. However, if issues persist or if <code>listen_addresses<\/code> was changed, a <code>restart<\/code> is necessary.<\/p><\/blockquote>\n\n<h4>7. Check Firewall Rules (firewalld)<\/h4>\n\n<p>While less likely to cause an &quot;authorization failed&quot; error (which implies the connection reached PostgreSQL), firewall rules can prevent connections entirely, leading to &quot;connection refused.&quot; It&#039;s a good practice to verify if you&#039;re troubleshooting any connection issue.<\/p>\n\n<p>PostgreSQL typically listens on port <code>5432<\/code>. Ensure this port is open on your CentOS Stream\/Rocky Linux server.<\/p>\n\n<pre><code class=\"language-bash\"># Check current firewall status<\/code><\/pre>\n\n<h1>If port 5432 is not listed, add it (for public zone, adjust if needed)\nsudo firewall-cmd &#8211;zone=public &#8211;add-port=5432\/tcp &#8211;permanent\nsudo firewall-cmd &#8211;reload\n<code><\/code>`<\/h1>\n\n<h4>8. Verify PostgreSQL User and Password<\/h4>\n\n<p>Ensure the database user exists and has the correct password set, matching the authentication method in <code>pg_hba.conf<\/code>.<\/p>\n\n<pre><code class=\"language-bash\"># Connect as the postgres superuser<\/code><\/pre>\n\n<h1>List users and their attributes (look for your user)\ndu<\/h1>\n\n<h1>If the user doesn&#039;t exist, create it:\nCREATE USER web<em>app<\/em>user WITH PASSWORD &#039;a<em>very<\/em>strong_password&#039; VALID UNTIL &#039;2028-01-01&#039;;<\/h1>\n\n<h1>If the password needs to be set\/reset (especially for scram-sha-256):\nALTER USER web<em>app<\/em>user WITH PASSWORD &#039;new<em>strong<\/em>password&#039;;<\/h1>\n\n<h1>Grant connect privileges to the database (if not already done)\nGRANT CONNECT ON DATABASE webapp<em>db TO web<\/em>app_user;<\/h1>\n\n<h1>Quit psql\nq\n<code><\/code>`<\/h1>\n\n<blockquote><p>[!IMPORTANT]\nPostgreSQL 10+ defaults to <code>scram-sha-256<\/code> for new password hashes. If your <code>pg<em>hba.conf<\/code> uses <code>md5<\/code> and the user password was created more recently, there might be a mismatch. You can explicitly set the password using <code>ALTER USER ... WITH PASSWORD ...<\/code> and ensure <code>pg<\/em>hba.conf<\/code> matches.<\/p><\/blockquote>\n\n<h4>9. Test the Connection<\/h4>\n\n<p>After making all changes and reloading\/restarting PostgreSQL, attempt to connect again from your client or application.<\/p>\n\n<pre><code class=\"language-bash\"># From the client machine or server itself\npsql -h your_db_host -U your_db_user -d your_db_name<\/code><\/pre>\n\n<p>If successful, you should be prompted for a password (if using <code>scram-sha-256<\/code> or <code>md5<\/code>) and then connect to the database. If the error persists, carefully review the PostgreSQL logs for the exact <code>FATAL<\/code> message and re-check each step, paying close attention to IP addresses, user names, database names, and authentication methods in your <code>pg_hba.conf<\/code> file.<\/p>","protected":false},"excerpt":{"rendered":"<p>Troubleshoot and resolve &#8216;connection authorization failed&#8217; errors in PostgreSQL on CentOS Stream and Rocky Linux by correctly configuring pg_hba.conf and related settings.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[187],"tags":[280,242,279,275,150,172,6],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-database","tag-authorization-failed","tag-centos-stream","tag-database-connection","tag-pg_hba-conf","tag-postgresql","tag-rocky-linux","tag-troubleshooting"],"_links":{"self":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=107"}],"version-history":[{"count":1,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions\/226"}],"wp:attachment":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}