1
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?

#Render × Supabase で `sqlalche.me/e/20/e3q8` エラー!解決策は Session Pooler!

Posted at

🚀 Render で Flask アプリをデプロイ → Supabase に接続 → エラー発生!

Render で Flask アプリをデプロイし、Supabase の PostgreSQL に接続しようとしたら、以下のエラーにハマった。

(Background on this error at: https://sqlalche.me/e/20/e3q8)

何度も何度も環境変数を確認したが、エラーは消えない。
そんな中、Render のログを細かく見ていたら、「Network is unreachable」 というメッセージを発見!

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "db.gszvzrujgenrgtyllcuj.supabase.co" (2406:da18:243:7408:915f:f242:b28b:aad0), port 5432 failed: Network is unreachable

💡 原因:Supabase はデフォルトで「IPv6 のみ」!

調べてみると、Supabase のデフォルト DB は IPv6 のみ で動作している。
一方、Render のサーバーは IPv4 しか使えない!
そのため、Render から直接 Supabase に接続しようとすると 「ネットワークに到達できない」エラー が出る。

✅ 解決策:Session Pooler を使う!

Supabase では 「Session Pooler」 を使うことで、IPv4 からでも接続できるようになる。

やることは簡単!

  1. Supabase の「Session Pooler」の接続 URL を取得
  2. Render の DATABASE_URL を変更
  3. 再デプロイするだけ!

🚀 具体的な解決方法

1. Supabase の「Session Pooler」URL を取得

Supabase のダッシュボードで 「Database」→「Settings」→「Session Pooler」 を開き、
そこに記載されている 「IPv4 互換の URL」 をコピー。

2. Render の DATABASE_URL を変更

Render の「Environment Variables」で DATABASE_URL を、
Supabase の「Session Pooler」の URL に変更。

postgresql://postgres:【設定したパスワード】@session-pooler.gszvzrujgenrgtyllcuj.supabase.co:5432/postgres

3. 再デプロイ

  • Render で「Manual Deploy」→「Deploy latest commit」
  • Flask アプリが正しく Supabase に接続できるか確認!

🔍 その他にやったこと(念のため)

✅ 1. SSL 接続の設定

念のため、DATABASE_URL?sslmode=require を追加。

if "sslmode" not in DATABASE_URL:
    DATABASE_URL += "?sslmode=require"

✅ 2. postgres://postgresql:// の変換

Supabase の DATABASE_URLpostgres:// になっていることがあるので、
SQLAlchemy で使えるように postgresql:// に変換。

if DATABASE_URL.startswith("postgres://"):
    DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)

🎉 結果

無事に Supabase の PostgreSQL に Render から接続成功! 🎉

同じ問題にハマった人は、ぜひ Session Pooler を試してみてください! 🚀

1
0
1

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
1
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?