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$;
Something like this (using dblink instead of postgres_fdw) allows to check if a db exists before creating it:
https://www.postgresql.org/docs/current/postgres-fdw.html
postgres-fdw
does not come with postgres. It's in thepostgres-contrib
package