You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否将SQLAlchemy与Hasura Postgres BaaS集成?数据库URL是什么?

Can SQLAlchemy integrate with Hasura Postgres BaaS?

Absolutely! Hasura Postgres BaaS is just a managed PostgreSQL instance at its core, and SQLAlchemy has first-class support for PostgreSQL. This integration works seamlessly—you can use SQLAlchemy's ORM or core API to handle complex data operations, while still leveraging Hasura's GraphQL layer for quick API setup and access. It’s a fantastic combo if you need both the flexibility of SQLAlchemy for custom data logic and Hasura’s rapid GraphQL capabilities.

How to get/configure the database URL for integration

Getting the right database URL from Hasura and setting it up for SQLAlchemy is straightforward. Here’s a step-by-step breakdown:

  1. Retrieve the connection string from Hasura Cloud

    • Log into your Hasura Cloud dashboard and pick your target project.
    • Head to the Data tab, then select your connected PostgreSQL database.
    • Look for the Connection String section (you might need to click "Show" to reveal the full string, including the password).
    • The string follows the standard PostgreSQL format: postgresql://<username>:<password>@<host>:<port>/<database-name>
  2. Add SSL configuration (critical for Hasura's managed Postgres)

    • Hasura’s Postgres instances almost always require SSL connections. To make sure SQLAlchemy connects without issues, append ?sslmode=require to the end of your URL. For example:
      postgresql://admin:your-secure-password@db.hasura.io:5432/your-db-name?sslmode=require
      
  3. Set up SQLAlchemy with the URL

    • Use this formatted URL to create a SQLAlchemy engine. Here’s a quick code snippet to get you started:
      from sqlalchemy import create_engine
      from sqlalchemy.ext.declarative import declarative_base
      from sqlalchemy.orm import sessionmaker
      
      DATABASE_URL = "postgresql://admin:your-password@db.hasura.io:5432/your-db-name?sslmode=require"
      
      engine = create_engine(DATABASE_URL)
      SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
      Base = declarative_base()
      
  4. Verify user permissions

    • Ensure the database user in your connection string has the necessary permissions (like SELECT, INSERT, UPDATE, DELETE) on the tables you want to work with via SQLAlchemy. You can manage these permissions in the Hasura dashboard under Data > [Your Database] > Roles.

内容的提问来源于stack exchange,提问作者ashwani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:48:14