Better-auth cannot be set up using pg Pool and postgres

It seems somewhere in the code there's hardcoded user relation when trying to set up: database: new Pool({ connectionString: process.env.DATABASE_URL, }), tables: { user: "users", account: "account", session: "session", verification: "verification" }, re-mapping has no effect, you still get an error:
2025-02-27T20:29:35.974Z ERROR [Better Auth]: Better auth was unable to query your database.
Error: [error: relation "user" does not exist] {
length: 103,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '15',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '1452',
routine: 'parserOpenTable'
}
GET /api/auth/callback/google?state=MAMMsYQKDb35w4C1JT8jGjqf6NXPecCy&code=4%2F0AQSTgQF4TIZX_veUgaUBV0f5hQ_AkYIuNYRfKKQkeM49kg1oUnAozryq-X5hlGjCawOePg&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=none 302 in 557ms
GET /api/auth/error?error=internal_server_error 200 in 68ms
2025-02-27T20:29:35.974Z ERROR [Better Auth]: Better auth was unable to query your database.
Error: [error: relation "user" does not exist] {
length: 103,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '15',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '1452',
routine: 'parserOpenTable'
}
GET /api/auth/callback/google?state=MAMMsYQKDb35w4C1JT8jGjqf6NXPecCy&code=4%2F0AQSTgQF4TIZX_veUgaUBV0f5hQ_AkYIuNYRfKKQkeM49kg1oUnAozryq-X5hlGjCawOePg&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=none 302 in 557ms
GET /api/auth/error?error=internal_server_error 200 in 68ms
When you use the npx @better-auth/cli generate command, it generates a migration file with "user" table and "user" relations but you cannot create a user table in postgres, it's reserved word. But when trying to log in, it still looks for the user table, dismissing the remapping to "users" table in auth.ts user: { modelName: "users", this gives me error:
2025-02-27T21:22:31.581Z ERROR [Better Auth]: unable_to_create_user
2025-02-27T21:22:31.581Z ERROR [Better Auth]: unable_to_create_user
3 Replies
King Alastor
King AlastorOP2mo ago
nothing?
bekacru
bekacru2mo ago
can you share your auth config
King Alastor
King AlastorOP2mo ago
better-auth.config.ts
export { auth } from './src/lib/auth/auth';
export { auth } from './src/lib/auth/auth';
and here's auth.ts
import { betterAuth } from "better-auth";
import { getUserCount } from "../data/auth-data";
import { Pool } from "pg";

export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
socialProviders: {
google: {
clientId: process.env.AUTH_GOOGLE_ID || "",
clientSecret: process.env.AUTH_GOOGLE_SECRET || "",
},
},
session: {
expiresIn: 60 * 60 * 24 * 30,
updateAge: 60 * 60 * 24,
cookieCache: {
enabled: true,
maxAge: 60 * 60 * 24,
},
},
user: {
modelName: "users",
additionalFields: {
userName: {
type: "string",
required: false,
},
// more fields
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
// stuff here
},
},
},
},
});
import { betterAuth } from "better-auth";
import { getUserCount } from "../data/auth-data";
import { Pool } from "pg";

export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
socialProviders: {
google: {
clientId: process.env.AUTH_GOOGLE_ID || "",
clientSecret: process.env.AUTH_GOOGLE_SECRET || "",
},
},
session: {
expiresIn: 60 * 60 * 24 * 30,
updateAge: 60 * 60 * 24,
cookieCache: {
enabled: true,
maxAge: 60 * 60 * 24,
},
},
user: {
modelName: "users",
additionalFields: {
userName: {
type: "string",
required: false,
},
// more fields
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
// stuff here
},
},
},
},
});
and here's the auth-client
import { inferAdditionalFields } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import type { auth } from "./auth";


export const authClient = createAuthClient({
plugins: [inferAdditionalFields<typeof auth>()],
});

export const { signIn, signOut, useSession } = authClient;
import { inferAdditionalFields } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import type { auth } from "./auth";


export const authClient = createAuthClient({
plugins: [inferAdditionalFields<typeof auth>()],
});

export const { signIn, signOut, useSession } = authClient;
@bekacru need anything else?

Did you find this page helpful?