{"record":{"id":"cee922f2afe36d59","repo":"transact-rs/sqlx","slug":"database-url-must-be-set-cee922","errorCode":null,"errorMessage":"DATABASE_URL must be set","messagePattern":"DATABASE_URL must be set","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sqlx-postgres/src/testing/mod.rs","lineNumber":42,"sourceCode":"impl TestSupport for Postgres {\n    fn test_context(\n        args: &TestArgs,\n    ) -> impl Future<Output = Result<TestContext<Self>, Error>> + Send + '_ {\n        test_context(args)\n    }\n\n    async fn cleanup_test(db_name: &str) -> Result<(), Error> {\n        let mut conn = MASTER_POOL\n            .get()\n            .expect(\"cleanup_test() invoked outside `#[sqlx::test]`\")\n            .acquire()\n            .await?;\n\n        do_cleanup(&mut conn, db_name).await\n    }\n\n    async fn cleanup_test_dbs() -> Result<Option<usize>, Error> {\n        let url = dotenvy::var(\"DATABASE_URL\").expect(\"DATABASE_URL must be set\");\n\n        let mut conn = PgConnection::connect(&url).await?;\n\n        let delete_db_names: Vec<String> = query_scalar(\"select db_name from _sqlx_test.databases\")\n            .fetch_all(&mut conn)\n            .await?;\n\n        if delete_db_names.is_empty() {\n            return Ok(None);\n        }\n\n        let mut deleted_db_names = Vec::with_capacity(delete_db_names.len());\n\n        let mut builder = QueryBuilder::new(\"drop database if exists \");\n\n        for db_name in &delete_db_names {\n            builder.push(db_name);\n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/transact-rs/sqlx/blob/03af8bcc5711a1935580a54bea249c219a0c217d/sqlx-postgres/src/testing/mod.rs#L24-L60","documentation":"This is a panic (via expect) raised during sqlx's test harness startup when `#[sqlx::test]` needs to clean up leftover test databases. The cleanup routine requires a live PostgreSQL connection string to reach the master database, and it reads that connection from the DATABASE_URL environment variable. It fires when the variable is not set in the environment (and not present in a .env file, since dotenvy::var is used), meaning no Postgres server is configured for the test run. Set DATABASE_URL to a Postgres connection URL with master-database privileges before running tests.","triggerScenarios":"Invoking `cleanup_test_dbs` without DATABASE_URL in env and without a `.env` file in the process working directory.","commonSituations":"CI missing the DATABASE_URL variable; running tests from a directory without the `.env`; renaming the env var or forgetting to source it after shell re-login.","solutions":["Provide `.env` at the crate root: `DATABASE_URL=postgres://user:pass@localhost/postgres`.","Export DATABASE_URL in the shell or CI job before running tests.","Run tests from the workspace/crate directory containing `.env`.","Confirm dotenvy is reachable (feature enabled) if you rely on `.env` loading."],"exampleFix":"// .env\n// before (missing)\n// after\nDATABASE_URL=postgres://postgres:password@localhost/postgres","handlingStrategy":"validation","validationCode":"let url = std::env::var(\"DATABASE_URL\")\n    .or_else(|_| dotenvy::var(\"DATABASE_URL\"))\n    .unwrap_or_else(|_| panic!(\"DATABASE_URL must be set (env or .env) before cleanup_test_dbs\"));\nassert!(url.starts_with(\"postgres://\") || url.starts_with(\"postgresql://\"));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Provide .env at the crate root and document it in the README","Set DATABASE_URL in CI before cargo test","Keep the env var name consistent across scripts and CI config"],"tags":["sqlx","postgres","testing","env-var","panic"],"backgroundTag":"missing-env-var","analyzedSha":"03af8bcc5711a1935580a54bea249c219a0c217d","analyzedAt":"2026-09-03T15:01:28.752Z","contentChangedAt":"2026-09-03T15:01:28.752Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}