diff options
author | 2021-09-07 09:59:18 +0100 | |
---|---|---|
committer | 2021-09-07 10:02:45 +0100 | |
commit | d245bbd600811762a74544aeff6086a6c94dd7f3 (patch) | |
tree | 64a5e8a1852af184211e86f08ffeda7a4326f206 /postgres | |
parent | Close db conns when finished (diff) |
Create and populate metricity in a single connection
Diffstat (limited to 'postgres')
-rw-r--r-- | postgres/init.sql | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/postgres/init.sql b/postgres/init.sql index ea748480..ee1acd8d 100644 --- a/postgres/init.sql +++ b/postgres/init.sql @@ -1,3 +1,24 @@ +DO +$do$ +DECLARE + _db TEXT := %s; + _user TEXT := %s; + _password TEXT := %s; +BEGIN + CREATE EXTENSION IF NOT EXISTS dblink; + IF EXISTS (SELECT 1 FROM pg_database WHERE datname = _db) THEN + RAISE NOTICE 'Database already exists'; + ELSE + PERFORM dblink_connect( + 'host=localhost user=' || _user || + ' password=' || _password || + ' dbname=' || current_database() + ); + PERFORM dblink_exec('CREATE DATABASE ' || _db); + END IF; +END +$do$; + CREATE TABLE IF NOT EXISTS users ( id varchar, joined_at timestamp, |