BA
Better Auth•3mo ago
pm

better-auth cli failing with ECONREFUSED

Hello i am having an issue initially setting up better auth for a trial. The cli generate command is failing with spurious looking errors. node: 22.17.1 nextjs: ^15.0.3 cli: latest DB: postgresql OS: MacOs 15.5 The auth is working successfully with memory adapter. The database connection seems to connect successfully to the db instance when running the app normally (i receive a cannot find db does not exsist error which is expected as i have not created the db yet). However when i run the cli generate command i get a couple of odd errors, weather i specify config location or not. I have tried adding the env vars to my env as the error seems to indicate they are not being set due to the starting warning of "Social provider microsoft is missing clientId or clientSecret" and that it is trying to connect to a DB on localhost and not the one specified. Logs and auth.ts in reply as too long for discord.
Solution:
Yeah it looks like it might be an ENV issue. Usually it's able to import .env files automatically tho, could it be that you have a different file name or using a different framework? šŸ¤”
Jump to solution
3 Replies
pm
pmOP•3mo ago
import { betterAuth } from "better-auth";
import { Pool } from "pg";


const postgresPool = new Pool({
user: process.env.DATABASE_USER as string,
password: process.env.DATABASE_PASS as string,
host: process.env.DATABASE_URL as string,
database: process.env.DATABASE_NAME as string,
})

export const auth = betterAuth({
database: postgresPool,
baseURL: process.env.BETTER_AUTH_URL as string,
secret: process.env.BETTER_AUTH_SECRET as string,
emailAndPassword: {
enabled: false // disable email password login
},
socialProviders: {
microsoft: {
clientId: process.env.SSO_CLIENT_ID as string,
clientSecret: process.env.SSO_CLIENT_SECRET as string,
tenantId: process.env.SSO_TENANT_ID as string,
enabled: true,
}
}
});
import { betterAuth } from "better-auth";
import { Pool } from "pg";


const postgresPool = new Pool({
user: process.env.DATABASE_USER as string,
password: process.env.DATABASE_PASS as string,
host: process.env.DATABASE_URL as string,
database: process.env.DATABASE_NAME as string,
})

export const auth = betterAuth({
database: postgresPool,
baseURL: process.env.BETTER_AUTH_URL as string,
secret: process.env.BETTER_AUTH_SECRET as string,
emailAndPassword: {
enabled: false // disable email password login
},
socialProviders: {
microsoft: {
clientId: process.env.SSO_CLIENT_ID as string,
clientSecret: process.env.SSO_CLIENT_SECRET as string,
tenantId: process.env.SSO_TENANT_ID as string,
enabled: true,
}
}
});
2025-07-22T11:07:12.087Z WARN [Better Auth]: Social provider microsoft is missing clientId or clientSecret
ā ‹ preparing schema.../Users/me/gitrepos/my-repo/app/node_modules/pg-pool/index.js:45
Error.captureStackTrace(err)
^

AggregateError [ECONNREFUSED]:
at /Users/me/gitrepos/myrepo/app/node_modules/pg-pool/index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async PostgresDriver.acquireConnection (

...snip...

at async generateMigrations (file:///Users/me/.npm/_npx/05eee8cc65087e98/node_modules/@better-auth/cli/dist/index.mjs:724:33) {
code: 'ECONNREFUSED',
[errors]: [
Error: connect ECONNREFUSED ::1:5432
at createConnectionError (node:net:1678:14)
at afterConnectMultiple (node:net:1708:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 5432
},
Error: connect ECONNREFUSED 127.0.0.1:5432
at createConnectionError (node:net:1678:14)
at afterConnectMultiple (node:net:1708:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
]
}
2025-07-22T11:07:12.087Z WARN [Better Auth]: Social provider microsoft is missing clientId or clientSecret
ā ‹ preparing schema.../Users/me/gitrepos/my-repo/app/node_modules/pg-pool/index.js:45
Error.captureStackTrace(err)
^

AggregateError [ECONNREFUSED]:
at /Users/me/gitrepos/myrepo/app/node_modules/pg-pool/index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async PostgresDriver.acquireConnection (

...snip...

at async generateMigrations (file:///Users/me/.npm/_npx/05eee8cc65087e98/node_modules/@better-auth/cli/dist/index.mjs:724:33) {
code: 'ECONNREFUSED',
[errors]: [
Error: connect ECONNREFUSED ::1:5432
at createConnectionError (node:net:1678:14)
at afterConnectMultiple (node:net:1708:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 5432
},
Error: connect ECONNREFUSED 127.0.0.1:5432
at createConnectionError (node:net:1678:14)
at afterConnectMultiple (node:net:1708:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
]
}
Solution
Ping
Ping•3mo ago
Yeah it looks like it might be an ENV issue. Usually it's able to import .env files automatically tho, could it be that you have a different file name or using a different framework? šŸ¤”
pm
pmOP•3mo ago
Hello, Thanks that instantly solves this for me! i am using ".env.local" as my env file name. Thank you for your support. The Package is working great so far except this niggle. keep up the great work!

Did you find this page helpful?