You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下Tomcat连PostgreSQL报错:C3P0资源获取失败(改postgres_hba.conf后)

Fix: C3P0 Connection Pool Acquisition Failed After Modifying postgres_hba.conf (Linux + Tomcat + PostgreSQL)

Alright, let's break this down. That wall of C3P0 warning text boils down to one simple issue: PostgreSQL is rejecting connection requests from your Tomcat app, and since you just edited postgres_hba.conf—PostgreSQL's core access control file—your changes are almost certainly the culprit. Here's how to fix it step by step:

1. Double-Check Your postgres_hba.conf Configuration

First, find where this file lives. It's usually in PostgreSQL's data directory, but you can get the exact path with this command:

sudo -u postgres psql -c "show hba_file;"

Open it up and make sure you have a valid rule for your Tomcat server:

  • If Tomcat and PostgreSQL are on the same machine, add or update a line like this (use scram-sha-256 for newer PostgreSQL versions, md5 for older ones):
    host    all             all             127.0.0.1/32            scram-sha-256
    
  • If Tomcat is on a different server, replace 127.0.0.1/32 with your Tomcat server's IP or IP range (e.g., 192.168.1.0/24 for a local network).

Pro tip: Rule order matters! PostgreSQL checks rules top to bottom, so make sure your allow rule comes before any reject/deny rules.

2. Restart PostgreSQL to Apply Changes

Editing postgres_hba.conf doesn't take effect until you restart the service. Do that with:

# Debian/Ubuntu
sudo systemctl restart postgresql
# RHEL/CentOS (replace <version> with your PostgreSQL version, e.g., 14)
sudo systemctl restart postgresql-<version>

3. Test the Database Connection Directly

Skip Tomcat and C3P0 for a second—verify you can connect to PostgreSQL directly. This will tell you if the issue is with the database itself or your app config:

  • Same server:
    psql -U <your-db-username> -d <your-db-name>
    
  • Remote server:
    psql -h <postgres-server-ip> -U <your-db-username> -d <your-db-name>
    

If this fails, fix that first: check if your username/password is correct, or if the PostgreSQL port (default 5432) is open in the firewall.

4. Verify Your C3P0/Tomcat Connection Config

Make sure your app's database pool settings are correct (usually in context.xml or your app's config file):

  • Double-check the jdbcUrl—it should look like this:
    jdbc:postgresql://<postgres-ip>:5432/<your-db-name>
    
  • Ensure the user and password match what you used in the direct psql test.
  • For more debug info, enable detailed C3P0 logging by adding these properties to your pool config:
    <property name="debugUnreturnedConnectionStackTraces" value="true"/>
    <property name="maxAdministrativeTaskTime" value="10"/>
    

This will show you the exact failure reason (e.g., authentication error, timeout) instead of just the generic C3P0 warning.

5. Check Firewall Settings (If Remote)

If Tomcat is on a different server, make sure PostgreSQL's firewall allows incoming connections on port 5432 from Tomcat's IP:

# Debian/Ubuntu with ufw
sudo ufw allow from <tomcat-server-ip> to any port 5432
# RHEL/CentOS with firewalld
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="<tomcat-server-ip>" port port="5432" protocol="tcp" accept' --permanent
sudo firewall-cmd --reload

A quick reminder: Always test connections right after modifying postgres_hba.conf—it's easy to lock yourself out by accident. If you're still stuck, post the full error trace from C3P0 (not just the snippet) and we can dig deeper.

内容的提问来源于stack exchange,提问作者Akshay Kamble

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:26:48