From 8779585091aa208533ea6c75648e6f4664a3df34 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Sat, 28 Jun 2025 06:58:17 +0000 Subject: [PATCH] fix: resolve PostgreSQL authentication failure during instance creation The issue was caused by using peer authentication during initial setup, which requires the system user to match the PostgreSQL user. When running as 'samo' but trying to connect as 'postgres', peer authentication would fail. Changes: - Use 'trust' authentication during initdb instead of 'peer' - Switch to 'md5' authentication for local connections in pg_hba.conf - Add proper postgres superuser password setup during initial creation This resolves the "Peer authentication failed for user 'postgres'" error and allows successful database and user creation. Fixes #46 Co-authored-by: Samuel --- src/instance/manager.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/instance/manager.ts b/src/instance/manager.ts index 908a8a1..660d619 100644 --- a/src/instance/manager.ts +++ b/src/instance/manager.ts @@ -264,6 +264,11 @@ export class InstanceManager { console.log('PostgreSQL is ready, creating database and user...'); + // First, set a password for the postgres superuser + console.log('Setting password for postgres superuser...'); + const postgresPassword = this.generateSecurePassword(); + await this.execAsyncWithLogging(`${psqlPath} -h "${socketDirectory}" -p ${config.spec.network.port} -U postgres -d postgres -c "ALTER USER postgres PASSWORD '${postgresPassword}'"`); + // Create the database console.log(`Creating 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}\\""`); @@ -444,7 +449,7 @@ export class InstanceManager { const command = [ initdbPath, '-D', config.spec.storage.dataDirectory, - '--auth-local=peer', + '--auth-local=trust', '--auth-host=md5', `--encoding=${config.spec.database.encoding}`, `--locale=${config.spec.database.locale}`, @@ -644,8 +649,8 @@ export class InstanceManager { '# pg_hba.conf generated by PgForge', '# TYPE DATABASE USER ADDRESS METHOD', '', - '# Local connections', - 'local all all peer', + '# Local connections - use md5 for password authentication', + 'local all all md5', '', '# IPv4 connections', ];