SeaQL/sea-orm · error

Not Postgres Connection

Error message

Not Postgres Connection

What it means

get_postgres_connection_pool() returns the underlying sqlx::PgPool. It panics when the inner connection is not SqlxPostgresPoolConnection — i.e. the connection is MySQL, SQLite, mock, proxy, rusqlite, or Disconnected.

Source

Thrown at sea-orm-sync/src/database/db_connection.rs:795

    /// Panics if [DbConn] is not a MySQL connection.
    #[cfg(feature = "sqlx-mysql")]
    pub fn get_mysql_connection_pool(&self) -> &sqlx::MySqlPool {
        match &self.inner {
            DatabaseConnectionType::SqlxMySqlPoolConnection(conn) => &conn.pool,
            _ => panic!("Not MySQL Connection"),
        }
    }

    /// Get [sqlx::PgPool]
    ///
    /// # Panics
    ///
    /// Panics if [DbConn] is not a Postgres connection.
    #[cfg(feature = "sqlx-postgres")]
    pub fn get_postgres_connection_pool(&self) -> &sqlx::PgPool {
        match &self.inner {
            DatabaseConnectionType::SqlxPostgresPoolConnection(conn) => &conn.pool,
            _ => panic!("Not Postgres Connection"),
        }
    }

    /// Get [sqlx::SqlitePool]
    ///
    /// # Panics
    ///
    /// Panics if [DbConn] is not a SQLite connection.
    #[cfg(feature = "sqlx-sqlite")]
    pub fn get_sqlite_connection_pool(&self) -> &sqlx::SqlitePool {
        match &self.inner {
            DatabaseConnectionType::SqlxSqlitePoolConnection(conn) => &conn.pool,
            _ => panic!("Not SQLite Connection"),
        }
    }
}

impl DbBackend {

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Connect with a postgres:// (or postgresql://) URL with the sqlx-postgres feature enabled
  2. Guard with get_database_backend() == DbBackend::Postgres before unwrapping the pool
  3. Confirm the earlier connect() call succeeded and the connection was not discarded

Example fix

// before
let conn = Database::connect("mysql://...").await?;
let pool = conn.get_postgres_connection_pool(); // panics
// after
let conn = Database::connect("postgres://...").await?;
let pool = conn.get_postgres_connection_pool(); // ok
Defensive patterns

Strategy: validation

Validate before calling

assert_eq!(conn.get_database_backend(), DbBackend::Postgres, "raw pool access requires a Postgres connection");
let pool = conn.get_postgres_connection_pool();

Type guard

fn as_pg_pool(conn: &DatabaseConnection) -> Option<&sqlx::PgPool> {
    (conn.get_database_backend() == DbBackend::Postgres).then(|| conn.get_postgres_connection_pool())
}

Try / catch

let pool = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| conn.get_postgres_connection_pool()));
if pool.is_err() { return Err(anyhow!("not a Postgres connection")); }

Prevention

When it happens

Trigger: Calling get_postgres_connection_pool() on a connection made from a non-postgres:// URL, on a MockDatabase or proxy connection, or on a Disconnected connection.

Common situations: DATABASE_URL env var pointing at MySQL/SQLite while code assumes Postgres; tests using MockDatabase then reaching for the pool; connect() failed earlier leaving a Disconnected handle.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10). Data as JSON: /api/errors/86aebb3bdd2ef2d1. Report an issue: GitHub.