Skip to content

Commit

Permalink
use "postgres" as username during initdb PostgresApp#275
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jakob committed Nov 12, 2015
1 parent eceafbd commit 7d68494
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion Postgres/PostgresServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
}
Expand Down Expand Up @@ -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];
Expand All @@ -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"];
Expand Down

0 comments on commit 7d68494

Please sign in to comment.