Skip to content

Commit ef50b06

Browse files
committed
fix: Use fresh admin connection after DROP DATABASE
When dropping and recreating a database, the original target_client was connected to the database being dropped. After DROP DATABASE, PostgreSQL terminates that connection, causing subsequent CREATE DATABASE to fail with "connection closed". Now connects to 'postgres' admin database for CREATE DATABASE, mirroring the approach already used in drop_database_if_exists().
1 parent a353b7f commit ef50b06

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/commands/init.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,17 @@ pub async fn init(
480480
if should_drop {
481481
drop_database_if_exists(target_url, &db_info.name).await?;
482482

483-
// Recreate the database
483+
// Recreate the database using a fresh connection to 'postgres'
484+
// (target_client was connected to the dropped database and is now dead)
485+
let admin_url =
486+
replace_database_in_url(target_url, "postgres")?;
487+
let admin_client =
488+
postgres::connect_with_retry(&admin_url).await?;
484489
let create_query = format!(
485490
"CREATE DATABASE {}",
486491
crate::utils::quote_ident(&db_info.name)
487492
);
488-
target_client
493+
admin_client
489494
.execute(&create_query, &[])
490495
.await
491496
.with_context(|| {

0 commit comments

Comments
 (0)