From 7d684941355b84263c7128a159076e4223b8d41d Mon Sep 17 00:00:00 2001 From: Jakob Egger Date: Thu, 12 Nov 2015 12:26:17 +0100 Subject: [PATCH] use "postgres" as username during initdb #275 This change uses the user name "postgres" when calling initdb. This is the de facto standard used by other PostgreSQL distributions, and should make restoring from dumps more usable. To keep Postgres.app as simple to use, Postgres.app also creates a super user with the same name as the current system user, and creates a database with the same name. This makes sure that typing "psql" in terminal is all that's needed to connect to Postgres.app --- Postgres/PostgresServer.m | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/Postgres/PostgresServer.m b/Postgres/PostgresServer.m index a9e8076fe..65b8aec41 100644 --- a/Postgres/PostgresServer.m +++ b/Postgres/PostgresServer.m @@ -170,6 +170,12 @@ - (void)startWithCompletionHandler:(PostgresServerControlCompletionHandler)compl return; } + BOOL createdUser = [self createUserWithError:&error]; + if (!createdUser) { + if (completionBlock) dispatch_async(dispatch_get_main_queue(), ^{ completionBlock(NO, error); }); + return; + } + BOOL createdUserDatabase = [self createUserDatabaseWithError:&error]; if (completionBlock) dispatch_async(dispatch_get_main_queue(), ^{ completionBlock(createdUserDatabase, error); }); } @@ -301,7 +307,8 @@ -(BOOL)initDatabaseWithError:(NSError**)error { initdbTask.launchPath = [self.binPath stringByAppendingPathComponent:@"initdb"]; initdbTask.arguments = @[ /* data directory */ @"-D", self.varPath, - /* encoding */ @"-EUTF-8", + /* superuser name */ @"-U", @"postgres", + /* encoding */ @"--encoding=UTF-8", /* locale */ @"--locale=en_US.UTF-8" ]; initdbTask.standardError = [[NSPipe alloc] init]; @@ -319,6 +326,34 @@ -(BOOL)initDatabaseWithError:(NSError**)error { return initdbTask.terminationStatus == 0; } +-(BOOL)createUserWithError:(NSError**)error { + NSTask *task = [[NSTask alloc] init]; + task.launchPath = [self.binPath stringByAppendingPathComponent:@"createuser"]; + task.arguments = @[ + @"-U", @"postgres", + @"-p", @(self.port).stringValue, + @"--superuser", + NSUserName() + ]; + task.standardError = [[NSPipe alloc] init]; + [task launch]; + NSString *taskError = [[NSString alloc] initWithData:[[task.standardError fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding]; + [task waitUntilExit]; + + if (task.terminationStatus != 0 && error) { + NSMutableDictionary *errorUserInfo = [[NSMutableDictionary alloc] init]; + errorUserInfo[NSLocalizedDescriptionKey] = NSLocalizedString(@"Could not create default user.",nil); + errorUserInfo[NSLocalizedRecoverySuggestionErrorKey] = taskError; + errorUserInfo[NSLocalizedRecoveryOptionsErrorKey] = @[@"OK", @"Open Server Log"]; + errorUserInfo[NSRecoveryAttempterErrorKey] = [[RecoveryAttempter alloc] init]; + errorUserInfo[@"ServerLogRecoveryOptionIndex"] = @1; + errorUserInfo[@"ServerLogPath"] = self.logfilePath; + *error = [NSError errorWithDomain:@"com.postgresapp.Postgres.createuser" code:task.terminationStatus userInfo:errorUserInfo]; + } + + return task.terminationStatus == 0; +} + -(BOOL)createUserDatabaseWithError:(NSError**)error { NSTask *task = [[NSTask alloc] init]; task.launchPath = [self.binPath stringByAppendingPathComponent:@"createdb"];