Merge branch 'patch-1' of https://github.com/mr-c/arvados into mr-c-patch-1
[arvados.git] / lib / boot / postgresql.go
index a08180e0c39a51ad685c08fdb16707605fd33b20..34ccf04a88dbd68a7822cc75b13da972e32844ee 100644 (file)
@@ -81,8 +81,13 @@ func (runPostgreSQL) Run(ctx context.Context, fail func(error), super *Superviso
                if err != nil {
                        return err
                }
-               args = append([]string{"-u", "postgres", prog}, args...)
-               prog = "sudo"
+               // We can't use "sudo -u" here because it creates an
+               // intermediate process that interferes with our
+               // ability to reliably kill postgres. The setuidgid
+               // program just calls exec without forking, so it
+               // doesn't have this problem.
+               args = append([]string{"postgres", prog}, args...)
+               prog = "setuidgid"
        }
        err = super.RunProgram(ctx, super.tempdir, nil, nil, prog, args...)
        if err != nil {
@@ -112,8 +117,8 @@ func (runPostgreSQL) Run(ctx context.Context, fail func(error), super *Superviso
                        "-p", super.cluster.PostgreSQL.Connection["port"],
                }
                if iamroot {
-                       args = append([]string{"-u", "postgres", prog}, args...)
-                       prog = "sudo"
+                       args = append([]string{"postgres", prog}, args...)
+                       prog = "setuidgid"
                }
                fail(super.RunProgram(ctx, super.tempdir, nil, nil, prog, args...))
        }()
@@ -149,7 +154,7 @@ func (runPostgreSQL) Run(ctx context.Context, fail func(error), super *Superviso
        if err != nil {
                return fmt.Errorf("createuser failed: %s", err)
        }
-       _, err = conn.ExecContext(ctx, `CREATE DATABASE `+pq.QuoteIdentifier(super.cluster.PostgreSQL.Connection["dbname"]))
+       _, err = conn.ExecContext(ctx, `CREATE DATABASE `+pq.QuoteIdentifier(super.cluster.PostgreSQL.Connection["dbname"])+` WITH TEMPLATE template0 ENCODING 'utf8'`)
        if err != nil {
                return fmt.Errorf("createdb failed: %s", err)
        }