PostgreSQL connection error using pg in Next.js API routes
Unanswered
Grass Carrying Wasp posted this in #help-forum
Grass Carrying WaspOP
i’m building an e-commerce app with Next.js + Node, and I’m using PostgreSQL with the
I created my database/table through the PostgreSQL CLI, but I’m having trouble connecting to the database from my app.
Current setup:
* Next.js frontend / API routes
* Node backend
* PostgreSQL database installed using CLI - no password auth
*
What I’ve done so far:
* Installed
* Created the DB/table in PostgreSQL via CLI and verified table public.products
* Set up a connection string / DB config in my app
Issue:
When I try to connect or run a query from Node, it fails with this error:
Postgres snapshot:
<postgres=# SELECT * FROM public.products
postgres-# ;
id | name | sku | price_cents | created_at
----+-----------------+----------+-------------+-------------------------------
1 | Oatmilk & Honey | SOAP-001 | 1200 | 2026-04-02 18:39:11.324039-04
2 | Lavender | SOAP-002 | 1200 | 2026-04-02 18:39:11.324039-04
3 | Unscented | SOAP-003 | 1100 | 2026-04-02 18:39:11.324039-04>
Code snippet:
What I’m trying to figure out:
* if my
* if my PostgreSQL server is not accepting connections
* if I’m misunderstanding how to connect Node/Next.js to a local Postgres DB
Any guidance would be appreciated.
pg package.I created my database/table through the PostgreSQL CLI, but I’m having trouble connecting to the database from my app.
Current setup:
* Next.js frontend / API routes
* Node backend
* PostgreSQL database installed using CLI - no password auth
*
pg package for DB connectionWhat I’ve done so far:
* Installed
pg* Created the DB/table in PostgreSQL via CLI and verified table public.products
* Set up a connection string / DB config in my app
Issue:
When I try to connect or run a query from Node, it fails with this error:
GET /api/products 500 in 238ms (compile: 53ms, render: 185ms)
Failed to fetch products error: relation "public.products" does not existPostgres snapshot:
<postgres=# SELECT * FROM public.products
postgres-# ;
id | name | sku | price_cents | created_at
----+-----------------+----------+-------------+-------------------------------
1 | Oatmilk & Honey | SOAP-001 | 1200 | 2026-04-02 18:39:11.324039-04
2 | Lavender | SOAP-002 | 1200 | 2026-04-02 18:39:11.324039-04
3 | Unscented | SOAP-003 | 1100 | 2026-04-02 18:39:11.324039-04>
Code snippet:
import { Client, Pool } from 'pg';
export function getClient(): Client {
const client = new Client(
{
connectionString: DATABASE_URL,
host: "localhost",
user: "drew",
port: 5432,
password: LOCAL_PASSWORD,
},
);
return client;
}
const response = await client.query('SELECT * FROM public.products');
console.log('DB URL at runtime:', process.env.DATABASE_URL);
console.log(response.rows);What I’m trying to figure out:
* if my
pg connection config is wrong* if my PostgreSQL server is not accepting connections
* if I’m misunderstanding how to connect Node/Next.js to a local Postgres DB
Any guidance would be appreciated.