Skip to content

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

@Dianadec

Description

@Dianadec

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions