+static void
+drop_database_if_exists(const char *dbname)
+{
+ header(_("dropping database \"%s\""), dbname);
+ psql_command("postgres", "DROP DATABASE IF EXISTS \"%s\"", dbname);
+}
+
+static void
+create_database(const char *dbname)
+{
+ _stringlist *sl;
+
+ /*
+ * We use template0 so that any installation-local cruft in template1 will
+ * not mess up the tests.
+ */
+ header(_("creating database \"%s\""), dbname);
+ if (encoding)
+ psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0 ENCODING='%s'%s", dbname, encoding,
+ (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
+ else
+ psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0%s", dbname,
+ (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
+ psql_command(dbname,
+ "ALTER DATABASE \"%s\" SET lc_messages TO 'C';"
+ "ALTER DATABASE \"%s\" SET lc_monetary TO 'C';"
+ "ALTER DATABASE \"%s\" SET lc_numeric TO 'C';"
+ "ALTER DATABASE \"%s\" SET lc_time TO 'C';"
+ "ALTER DATABASE \"%s\" SET timezone_abbreviations TO 'Default';",
+ dbname, dbname, dbname, dbname, dbname);
+
+ /*
+ * Install any requested procedural languages. We use CREATE OR REPLACE
+ * so that this will work whether or not the language is preinstalled.
+ */
+ for (sl = loadlanguage; sl != NULL; sl = sl->next)
+ {
+ header(_("installing %s"), sl->str);
+ psql_command(dbname, "CREATE OR REPLACE LANGUAGE \"%s\"", sl->str);
+ }
+
+ /*
+ * Install any requested extensions. We use CREATE IF NOT EXISTS so that
+ * this will work whether or not the extension is preinstalled.
+ */
+ for (sl = loadextension; sl != NULL; sl = sl->next)
+ {
+ header(_("installing %s"), sl->str);
+ psql_command(dbname, "CREATE EXTENSION IF NOT EXISTS \"%s\"", sl->str);
+ }
+}
+
+static void
+drop_role_if_exists(const char *rolename)
+{
+ header(_("dropping role \"%s\""), rolename);
+ psql_command("postgres", "DROP ROLE IF EXISTS \"%s\"", rolename);
+}
+
+static void
+create_role(const char *rolename, const _stringlist * granted_dbs)
+{
+ header(_("creating role \"%s\""), rolename);
+ psql_command("postgres", "CREATE ROLE \"%s\" WITH LOGIN", rolename);
+ for (; granted_dbs != NULL; granted_dbs = granted_dbs->next)
+ {
+ psql_command("postgres", "GRANT ALL ON DATABASE \"%s\" TO \"%s\"",
+ granted_dbs->str, rolename);
+ }
+}
+
+static char *
+make_absolute_path(const char *in)
+{
+ char *result;
+
+ if (is_absolute_path(in))
+ result = strdup(in);
+ else
+ {
+ static char cwdbuf[MAXPGPATH];
+
+ if (!cwdbuf[0])
+ {
+ if (!getcwd(cwdbuf, sizeof(cwdbuf)))
+ {
+ fprintf(stderr, _("could not get current working directory: %s\n"), strerror(errno));
+ exit_nicely(2);
+ }
+ }
+
+ result = malloc(strlen(cwdbuf) + strlen(in) + 2);
+ sprintf(result, "%s/%s", cwdbuf, in);
+ }
+
+ canonicalize_path(result);
+ return result;
+}
+