From bedc68903929bb86386312cdbb2555478bc51caf Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Sat, 28 Jun 2025 06:51:35 +0000 Subject: [PATCH] fix: use Unix socket connections during PostgreSQL instance creation Fixes authentication issue where temporary PostgreSQL instance creation failed due to password prompts on TCP/IP connections. Changes: - Modified waitForPostgreSQLReady() to use Unix socket directory parameter - Updated all psql commands to use Unix socket (-h socketDirectory) instead of TCP/IP (-h 127.0.0.1) during temporary startup - This uses peer authentication (no password) instead of md5 (password required) The issue occurred because initdb sets --auth-host=md5 for TCP/IP connections but the postgres user doesn't have a password set during temporary startup. Unix socket connections use peer authentication which doesn't require passwords. Fixes #43 Co-authored-by: Samuel --- src/instance/manager.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/instance/manager.ts b/src/instance/manager.ts index 28dc62b..908a8a1 100644 --- a/src/instance/manager.ts +++ b/src/instance/manager.ts @@ -260,25 +260,25 @@ export class InstanceManager { console.log(`Temporary PostgreSQL process started with PID: ${tempProcess.pid}`); // Wait for PostgreSQL to start - await this.waitForPostgreSQLReady(config.spec.network.port, config.spec.version); + await this.waitForPostgreSQLReady(config.spec.network.port, config.spec.version, socketDirectory); console.log('PostgreSQL is ready, creating database and user...'); // Create the database console.log(`Creating database: ${config.spec.database.name}`); - await this.execAsyncWithLogging(`${psqlPath} -h 127.0.0.1 -p ${config.spec.network.port} -U postgres -d postgres -c "CREATE DATABASE \\"${config.spec.database.name}\\""`); + await this.execAsyncWithLogging(`${psqlPath} -h "${socketDirectory}" -p ${config.spec.network.port} -U postgres -d postgres -c "CREATE DATABASE \\"${config.spec.database.name}\\""`); // Create the user with password console.log(`Creating user: ${config.spec.database.owner}`); - await this.execAsyncWithLogging(`${psqlPath} -h 127.0.0.1 -p ${config.spec.network.port} -U postgres -d postgres -c "CREATE USER \\"${config.spec.database.owner}\\" WITH PASSWORD '${password}'"`); + await this.execAsyncWithLogging(`${psqlPath} -h "${socketDirectory}" -p ${config.spec.network.port} -U postgres -d postgres -c "CREATE USER \\"${config.spec.database.owner}\\" WITH PASSWORD '${password}'"`); // Grant privileges to the user on the database console.log(`Granting database privileges...`); - await this.execAsyncWithLogging(`${psqlPath} -h 127.0.0.1 -p ${config.spec.network.port} -U postgres -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE \\"${config.spec.database.name}\\" TO \\"${config.spec.database.owner}\\""`); + await this.execAsyncWithLogging(`${psqlPath} -h "${socketDirectory}" -p ${config.spec.network.port} -U postgres -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE \\"${config.spec.database.name}\\" TO \\"${config.spec.database.owner}\\""`); // Grant the user permission to create schemas in the database console.log(`Granting schema creation privileges...`); - await this.execAsyncWithLogging(`${psqlPath} -h 127.0.0.1 -p ${config.spec.network.port} -U postgres -d "${config.spec.database.name}" -c "GRANT CREATE ON SCHEMA public TO \\"${config.spec.database.owner}\\""`); + await this.execAsyncWithLogging(`${psqlPath} -h "${socketDirectory}" -p ${config.spec.network.port} -U postgres -d "${config.spec.database.name}" -c "GRANT CREATE ON SCHEMA public TO \\"${config.spec.database.owner}\\""`); console.log('Database and user created successfully'); @@ -356,7 +356,7 @@ export class InstanceManager { return password; } - private async waitForPostgreSQLReady(port: number, version: string, maxAttempts: number = 30): Promise { + private async waitForPostgreSQLReady(port: number, version: string, socketDirectory: string, maxAttempts: number = 30): Promise { const psqlPath = await this.findPostgreSQLBinary('psql', version); let lastError: any = null; @@ -364,7 +364,8 @@ export class InstanceManager { for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { - const command = `${psqlPath} -h 127.0.0.1 -p ${port} -U postgres -d postgres -c "SELECT 1"`; + // Use Unix socket connection to avoid password authentication during temporary startup + const command = `${psqlPath} -h "${socketDirectory}" -p ${port} -U postgres -d postgres -c "SELECT 1"`; console.log(`Attempt ${attempt}/${maxAttempts}: Testing PostgreSQL connection...`); await execAsync(command, {