bcgov / cas-postgres

Postgres container image intended for OpenShift and general usage
Apache License 2.0
2 stars 0 forks source link

cas-postgres: use postgres_fdw instead of handling errors in create_user_db #99

Open Dianadec opened 3 years ago

Dianadec commented 3 years ago

Something like this (using dblink instead of postgres_fdw) allows to check if a db exists before creating it:

DO
$do$
BEGIN
   IF EXISTS (SELECT FROM pg_database WHERE datname = 'mydb') THEN
      RAISE NOTICE 'Database already exists';  -- optional
   ELSE
      PERFORM dblink_exec('dbname=' || current_database()  -- current db
                        , 'CREATE DATABASE mydb');
   END IF;
END
$do$;

https://www.postgresql.org/docs/current/postgres-fdw.html postgres-fdw does not come with postgres. It's in the postgres-contrib package