// how to import client from pg
import { Client } from "pg";
import { NextResponse } from "next/server";
// how to create a client
const client = new Client({
database: "heritage",
user: "postgres",
password: "admin",
host: "localhost",
port: 5432,
});
// how to connect to the database
client.connect().then(() => {
console.log("connected");
});
// how to query the database
export async function GET() {
const res = await client.query("SELECT * FROM Products");
return NextResponse.json(res.rows);
}
GET /api/products 500 in 72ms
⨯ error: relation "products" does not exist
Assistant
The error message you're encountering:
code
error: relation "products" does not exist
indicates that PostgreSQL cannot find a table named products in your database. This issue often arises due to case sensitivity and identifier quoting in PostgreSQL.
Understanding PostgreSQL Identifier Case Sensitivity
In PostgreSQL, unquoted identifiers (like table and column names) are automatically converted to lowercase. For example, writing SELECT * FROM Products without quotes is interpreted as SELECT * FROM products.
If your table was created without quotes:
sql
CREATE TABLE Products (...);
PostgreSQL actually creates it as products in lowercase.
However, if your table was created with double quotes:
sql
CREATE TABLE "Products" (...);
Then PostgreSQL respects the casing, and the table name remains Products with an uppercase P. This table must always be referenced with double quotes and exact casing.
Solutions
Option 1: Adjust Your Query to Match the Table Name
If your table was created without quotes (default behavior), it exists in lowercase (products). Update your query to:
javascript
const res = await client.query('SELECT * FROM products');
If your table was created with quotes and has a capital P, you need to include double quotes in your query:
javascript
const res = await client.query('SELECT * FROM "Products"');
Option 2: Check if the Table Exists
Ensure that the table products (or Products) actually exists in your heritage database.
Connect to your database via psql or a database client and run:
sql
\dt
This command lists all tables in your current schema.
Verify the table name and casing. Make sure you're using the correct name in your query.
Option 3: Specify the Schema (If Applicable)
If your table exists in a specific schema other than public, you need to include the schema name:
javascript
const res = await client.query('SELECT * FROM schema_name.products');
Example Adjusted Code
javascript
import { Client } from "pg";
import { NextResponse } from "next/server";
// Create a client
const client = new Client({
database: "heritage",
user: "postgres",
password: "admin",
host: "localhost",
port: 5432,
});
// Connect to the database
await client.connect();
// Query the database
export async function GET() {
// Use the correct table name
const res = await client.query("SELECT * FROM products");
// or if the table name is capitalized and quoted
// const res = await client.query('SELECT * FROM "Products"');
return NextResponse.json(res.rows);
}
Additional Tips
Avoid Using Quoted Identifiers When Creating Tables: Unless necessary, it's a good practice to avoid using quoted identifiers in PostgreSQL to prevent case sensitivity issues.
Consistent Naming Conventions: Adopt a consistent naming convention (e.g., all lowercase with underscores) for database objects to minimize confusion.
Conclusion
The error stems from a mismatch between how PostgreSQL interprets unquoted identifiers and the actual table name in your database. By adjusting your query to match the exact table name and casing, you should be able to resolve the error.