Getting error while using Postgresql only

This is how I have set up the database:
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
I used better auth cli to generate and migrate the tables to postgresql. And when I make an api request to register, I get this error.
{
"error": "relation \"user\" does not exist"
}
{
"error": "relation \"user\" does not exist"
}
I did use the default public schema. so i'm not sure why am i getting this error. Here is the generated tables: List of relations Schema | Name | Type | Owner --------+--------------+-------+--------- public | account | table | testing public | session | table | testing public | user | table | testing public | verification | table | testing
7 Replies
Ping
Ping10h ago
can you make sure your database url is correct? (For example make sure you don't append anything after the connection string as that will link to a new database directory in pg - unless this is intentional for you)
doomer
doomerOP3h ago
It's correct. That's why better auth CLI successfully migrates the tables. And I have manually checked the database too. The tables exist inside the database. I didn't append anything either.
Ping
Ping3h ago
Just started a new project, followed our installation setup, ran migrations, everything seems to work for me. I might need more information on your setup if you could provide.
doomer
doomerOP3h ago
while learning, I used sqlite3. Everything worked fine with it. But with postgresql, I'm facing this issue.
import { betterAuth } from "better-auth";
import { createAuthMiddleware } from "better-auth/api";
import { Pool } from "pg";
// import Database from "better-sqlite3";

// Helper function to filter user data to only include fields from your User entity
const filterUserData = (user: any) => ({
id: user.id,
name: user.name,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
// Excluding emailVerified and image fields that you don't need
});

export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
emailAndPassword: {
enabled: true,
},
session: {
cookieCache: {
enabled: true,
maxAge: 60 * 60 * 24 * 7, // 7 days
},
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (
ctx.path.startsWith("/sign-up") ||
ctx.path.startsWith("/sign-in") ||
ctx.path.startsWith("/session")
) {
const returned = ctx.context.returned;

// Only modify if there's a user in the response
if (returned && typeof returned === "object" && "user" in returned) {
return ctx.json({
...returned,
user: filterUserData(returned.user),
});
}
}
}),
},
});
import { betterAuth } from "better-auth";
import { createAuthMiddleware } from "better-auth/api";
import { Pool } from "pg";
// import Database from "better-sqlite3";

// Helper function to filter user data to only include fields from your User entity
const filterUserData = (user: any) => ({
id: user.id,
name: user.name,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
// Excluding emailVerified and image fields that you don't need
});

export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
emailAndPassword: {
enabled: true,
},
session: {
cookieCache: {
enabled: true,
maxAge: 60 * 60 * 24 * 7, // 7 days
},
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (
ctx.path.startsWith("/sign-up") ||
ctx.path.startsWith("/sign-in") ||
ctx.path.startsWith("/session")
) {
const returned = ctx.context.returned;

// Only modify if there's a user in the response
if (returned && typeof returned === "object" && "user" in returned) {
return ctx.json({
...returned,
user: filterUserData(returned.user),
});
}
}
}),
},
});
Ping
Ping3h ago
Are you using docker to start the pg server? I can provide you how I set mine up so that maybe you can get an idea if there is any differences.
Ping
Ping3h ago
These three combined works on my end
No description
No description
No description
doomer
doomerOP3h ago
I am using the postgresql app to run the database servers. I'm not using docker. could this be the reason?

Did you find this page helpful?