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

Render托管PostgreSQL数据库在Node Express连接时抛ECONNRESET错误

Fixes for Render PostgreSQL ECONNRESET Error with Node.js postgres Package

Let's walk through the most likely fixes for your connection issue:

1. Reuse the Database Connection (Don't Create a New Client Per Query)

Your current code spins up a new postgres client every time you run a query. This is inefficient and can trigger connection limits or reset errors on Render's PostgreSQL service. Render expects persistent connections, so initialize your client once at startup and reuse it:

import postgres, { RowList, Row } from 'postgres';
import appconfig from '../app.config';

// Initialize client once when the module loads
const dbClient = postgres({
  host: appconfig.database.host,
  port: appconfig.database.port,
  database: appconfig.database.dbName, // Fix: Use actual database name, not schema
  username: appconfig.database.username,
  password: appconfig.database.password,
  ssl: true, // Required for external connections to Render PostgreSQL
});

type Query = (sql: string) => Promise<RowList<Row[]>>;

const query: Query = async (sql) => {
  try {
    const res = await dbClient`${sql}`;
    return res;
  } catch (err) {
    throw err;
  }
};

export default query;

2. Fix the database Parameter (Critical PostgreSQL Difference from MySQL)

Since you're coming from MySQL, you probably mixed up "database" and "schema"—a common pitfall. In PostgreSQL:

  • The database field refers to the top-level database instance (you can find this in your Render database dashboard, e.g., dpg-xyz12345...).
  • Schemas are namespaces inside the database (the default is public).
    Replace appconfig.database.schema with your actual Render database name in the client configuration.

3. Enable SSL Connections

Render requires SSL for all external PostgreSQL connections. The postgres npm package doesn't enable this by default, but PGAdmin does—which explains why PGAdmin works but your code doesn't. Add ssl: true to your client options. If you run into certificate verification issues (unlikely with Render), you can use:

ssl: { rejectUnauthorized: false }

Stick with ssl: true first, though, since Render uses valid, trusted certificates.

4. Double-Check Credentials Match PGAdmin

Confirm every detail matches what you used in PGAdmin:

  • Host is exactly the Render database hostname (no typos).
  • Port is 5432 (Render's default PostgreSQL port).
  • Username and password are correct.
  • Database name is the top-level database, not a schema.

Quick Sanity Checks

  • Make sure your Render database's "Allow external connections" toggle is enabled (it should be on by default, but verify in the dashboard).
  • In production, use Render's environment variables for credentials instead of config files (not the cause of your current error, but essential for security).

These fixes should resolve the ECONNRESET error. The most common culprits here are missing SSL, incorrect database parameter, or creating too many short-lived connections.

内容的提问来源于stack exchange,提问作者Brandon Carr

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 12:45:36