
-- Execute the following SQL script as a database superuser

DO
$body$
  begin
  IF NOT EXISTS (SELECT * FROM pg_catalog.pg_user WHERE  usename = 'DB_OWNER')
  THEN
    create user DB_OWNER;
  END IF;
END;
$body$;

create database DB_NAME with owner DB_OWNER encoding 'UTF8' template=template0 tablespace=pg_default;
alter database DB_NAME set default_with_oids = off; 
alter database DB_NAME set join_collapse_limit = '32'; 
alter database DB_NAME set from_collapse_limit = '32'; 

\connect DB_NAME

begin;
create table database_version_logs(db_version varchar(4) not null, module_name varchar(250), run_status varchar(10), run_status_timestamp timestamp, module_code bytea, run_exception bytea);
insert into database_version_logs(db_version, run_status, run_status_timestamp) values ('0', 'SUCCESS', now());
alter database DB_NAME owner to DB_OWNER;
alter table database_version_logs owner to DB_OWNER;
commit;

