Skip to content

Commit

Permalink
Create PostgreSQL extensions conditionally.
Browse files Browse the repository at this point in the history
This is because in Azure PostgreSQL Flexible Server v15, ordinary users (without
the azure_pg_admin role) cannot create extensions. These extensions need to be
created before deploying this service into the cloud.
  • Loading branch information
jarkkoka committed Feb 21, 2025
1 parent e28dc72 commit f32bfc6
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/main/resources/db/migration/V2__init_schemas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ CREATE SCHEMA infrastructure_network;

CREATE SCHEMA network;

CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- Create extension conditionally. This is because in Azure PostgreSQL Flexible
-- Server v15, ordinary users (without the azure_pg_admin role) cannot create
-- extensions. These extensions need to be created before deploying this service
-- into the cloud.

CREATE EXTENSION IF NOT EXISTS btree_gist;
-- Conditionally create the btree_gist extension.
DO $$
DECLARE
ext_exists BOOLEAN;
BEGIN
-- Check if btree_gist extension exists.
SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'btree_gist') INTO ext_exists;

-- Create extension only if it doesn't exist.
IF NOT ext_exists THEN
CREATE EXTENSION IF NOT EXISTS btree_gist SCHEMA public;
END IF;
END $$;

-- Conditionally create the pgcrypto extension.
DO $$
DECLARE
ext_exists BOOLEAN;
BEGIN
-- Check if pgcrypto extension already exists.
SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'pgcrypto') INTO ext_exists;

-- Create extension only if it doesn't exist.
IF NOT ext_exists THEN
CREATE EXTENSION IF NOT EXISTS pgcrypto SCHEMA public;
END IF;
END $$;

0 comments on commit f32bfc6

Please sign in to comment.