-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensure_role_and_database_exists.c
51 lines (38 loc) · 1.75 KB
/
ensure_role_and_database_exists.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdlib.h>
#include "postgres.h"
#include "fmgr.h"
#include "libpq/auth.h"
#include <unistd.h>
PG_MODULE_MAGIC;
static ClientAuthentication_hook_type original_client_auth_hook = NULL;
static void ensure_role_and_database_exists(Port *port, int status) {
char *cmd;
char *postgres_user;
char *role_attributes;
if (original_client_auth_hook) {
original_client_auth_hook(port, status);
}
postgres_user = getenv("POSTGRES_USER");
fprintf(stderr, "handling connection for username '%s' to database '%s'\n", port->user_name, port->database_name);
// don't infinitely recurse when connecting as superuser
if (strcmp(port->database_name, postgres_user) == 0 && strcmp(port->user_name, postgres_user) == 0) {
return;
}
role_attributes = getenv("POSTGRES_ROLE_ATTRIBUTES");
fprintf(stderr, "ensuring user_name '%s' exists with attributes '%s'\n", port->user_name, role_attributes);
asprintf(&cmd,
"echo \"SELECT 'CREATE ROLE %s WITH %s' WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '%s')\\gexec\" | psql -U %s -d %s",
port->user_name, role_attributes, port->user_name, postgres_user, postgres_user);
system(cmd);
free(cmd);
fprintf(stderr, "ensuring database '%s' exists\n", port->database_name);
asprintf(&cmd,
"echo \"SELECT 'CREATE DATABASE %s WITH OWNER = %s' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '%s')\\gexec\" | psql -U %s -d %s",
port->database_name, port->user_name, port->database_name, postgres_user, postgres_user);
system(cmd);
free(cmd);
}
void _PG_init(void) {
original_client_auth_hook = ClientAuthentication_hook;
ClientAuthentication_hook = ensure_role_and_database_exists;
}