0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Hono + Drizzleで「password authentication failed for user」が発生したときの解決方法

0
Posted at

はじめに

HonoとDrizzle ORMを使ってPostgreSQLに接続するアプリを開発していたところ、データベースへの接続時に認証エラーが発生しました。

問題

Todoリストを取得するAPIを実行したところ、以下のようなエラーが発生しました。

DrizzleQueryError: Failed query

PostgresError: password authentication failed for user "username"

code: '28P01'

解決方法

src/db/client.ts
+   import "dotenv/config";
    import postgres from "postgres";
    import { drizzle } from "drizzle-orm/postgres-js";
    import * as schema from "./schema.js";
    
    export const client = postgres(process.env.DATABASE_URL!);
    export const db = drizzle({ client, schema });

アプリが.envファイルを読み込めていませんでした。

おわりに

今回のエラーでは、認証エラーの内容からPostgreSQLのユーザー設定が間違っていると考えてしまいました。
しかし実際には、PostgreSQLの設定ではなく、アプリ側が環境変数を読み込めていなかったことが原因でした。

もし今後同様のエラーに遭遇した場合は、まず以下を確認しようと思います。

  1. .env が存在するか
  2. process.env.DATABASE_URL が取得できるか
  3. dotenv/config を読み込んでいるか
  4. サーバーを再起動したか
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?