PostgresError: password authentication failed for user.
I am getting error while trying to insert a new user to the DB.
27 Replies
Here is my
scheme.ts
file,
here is db.ts
,
here is migrate.ts
,
here is my drizzle.config.ts
,
I have a .env
file in which I have this,
I run postgresql
in a docker container, here is the docker-compose
,
now I wrote a simple query for inserting a new user,
and I have this,
when I run my project, it starts fine but as soon as I try to go the URL the server crashes and I get this error,
Am I doing something wrong? Not sure why its trying to authenticate using my system
user..
I can do psql postgres://drizzle:drizzle@localhost/drizzle
or docker exec -it drizzle-db psql -U drizzle -d drizzle
both works, I get dropped in the psql
shell.
migration ran successfully, so I'm guessing the problem is not with connection to DB?Are you sure
process.env.DB_URL
is loaded correctly?
Add a console log to drizzle.config.ts
and see if it prints the desired valueIf not, read on how env vars are handled by Vite:
https://vitejs.dev/guide/env-and-mode.html
If it wouldn't, how would the migration happen?
But I'll double check.
Oh! wait.. I just noticed.. its not
DB_URL
its DATABASE_URL
, I have no idea how it got changed..
in that case, I guess the migration one doesn't gets the URL from drizzle.config.ts
?The migration runs with Node.js, where
process.env
is available. Vite has a bit of it's own logic when handling environment variables
Well, it doesn't seem to as you've defined the client explicitly with:
drizzle.config.ts
,
but once again it crashes,
I see.
Can this migrate.ts
file inherit this from drizzle.config.ts
?I don't know to be honest. rn I tried to import the result of
defineConfig
but seems that dbCredentials
are not exposed
I didn't run it though
I have it defined separately also, and I don't think these will change anytime in the project lifespan tbh
Sometimes it's good to settle on "good enough"
Back to the issue, how do you load environment variables?
Do you have an env file with this var defined?
Try prefixing it with VITE_
and see if it helps when running with ViteYes, a
.env
file and I use the dotenv
package I think.
in which file? everywhere?Nope, just in
.env
file where you define the DATABASE_URL
and change in the db.ts
where you use it, and change it to:
you mean rename the
DATABASE_URL
to VITE_DATABASE_URL
in .env
file and use import.meta.env.VITE_DATABASE_URL
in db.ts
instead?
I would also have to change the same in drizzle.config.ts
as well I guess? otherwise how would it pick it up?
so I have now these 2 fields in .env
file,
yes
For now, let's just test out if it works for Vite
interesting, in
db.ts
I did,
and it looks like its not crashing anymore..
Soo as I suspected, I recommend giving thorough read to the linked docs
and I do infact have a new user,
Just be aware that there's a security implication, as if you bundle/build your application, all variables exposed with
VITE_
stay in client-side code
Which means that every browser can inspect the connection stringHow can I handle this better?
Build an API for that 😛
Do you use some frontend framework?
I am using sveltekit.
Hmm, I'm not that familiar with sveltekit, but I assume that there's a way to run this code server-side
Ah! I see what you mean.
I'll ask in svelte discord channel for this.
essentialy you don't won't to expose any confidential secret to be exposed to the client
So the drizzle client should be initialized on the server-side, and then the
VITE_
prefix can be removed thenI see. Perhaps I should move the drizzle client config to svelte's "backend" thing..
Definitely
I'm also learning sveltekit BTW. I chose this framework as I wanted to learn it.
Heard good things about Svelte. I'm a React/Next.js guy myself, but a lot of principles stay the same
Good luck with the project, I think you'll handle it from there
Thanks! @Kuba
np, happy to help