Source

Implementation

Provisioner Type

Resource Type

Flavor

Tool

postgres

The default postgres provisioner adds a postgres instance and then ensures that the required databases are created on startup.

Provisions a dedicated database on a shared PostgreSQL instance.

type: postgres
expected_outputs:
  - host
  - port
  - name
  - database
  - username
  - password

provisioners.yaml (view on GitHub) :

- uri: template://default-provisioners/postgres
  # By default, match all redis types regardless of class and id. If you want to override this, create another
  # provisioner definition with a higher priority.
  type: postgres
  description: Provisions a dedicated database on a shared PostgreSQL instance.
  # Init template has the random service name and password if needed later
  init: |
    randomServiceName: pg-{{ randAlphaNum 6 }}
    randomDatabase: db-{{ randAlpha 8 }}
    randomUsername: user-{{ randAlpha 8 }}
    randomPassword: {{ randAlphaNum 16 | quote }}
    sk: default-provisioners-postgres-instance
    publishPort: {{ dig "annotations" "compose.score.dev/publish-port" "0" .Metadata | quote }}    
  # The state for each database resource is a unique db name and credentials
  state: |
    database: {{ dig "database" .Init.randomDatabase .State | quote }}
    username: {{ dig "username" .Init.randomUsername .State | quote }}
    password: {{ dig "password" .Init.randomPassword .State | quote }}    
  # All instances agree on the shared state since there is no concurrency here
  shared: |
    {{ .Init.sk }}:
      instanceServiceName: {{ dig .Init.sk "instanceServiceName" .Init.randomServiceName .Shared | quote }}
      instancePassword: {{ dig .Init.sk "instancePassword" .Init.randomPassword .Shared | quote }}    
  # The outputs are the core database outputs. We output both name and database for broader compatibility.
  outputs: |
    host: {{ dig .Init.sk "instanceServiceName" "" .Shared }}
    port: 5432
    name: {{ .State.database }}
    database: {{ .State.database }}
    username: {{ .State.username }}
    password: {{ .State.password }}    
  # Write out an idempotent create script per database
  files: |
    {{ dig .Init.sk "instanceServiceName" "" .Shared }}-db-scripts/{{ .State.database }}.sql: |
      SELECT 'CREATE DATABASE "{{ .State.database }}"' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '{{ .State.database }}')\gexec
      SELECT $$CREATE USER "{{ .State.username }}" WITH PASSWORD '{{ .State.password }}'$$ WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '{{ .State.username }}')\gexec
      GRANT ALL PRIVILEGES ON DATABASE "{{ .State.database }}" TO "{{ .State.username }}";
      \connect "{{ .State.database }}";
      GRANT ALL ON SCHEMA public TO "{{ .State.username }}";    
  # Ensure the data volume exists
  volumes: |
    {{ dig .Init.sk "instanceServiceName" "" .Shared }}-data:
      driver: local    
  # Create 2 services, the first is the database itself, the second is the init container which runs the scripts
  services: |
    {{ dig .Init.sk "instanceServiceName" "" .Shared }}:
      image: mirror.gcr.io/postgres:17-alpine
      restart: always
      environment:
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: {{ dig .Init.sk "instancePassword" "" .Shared | quote }}
      {{ if ne .Init.publishPort "0" }}
      ports:
      - target: 5432
        published: {{ .Init.publishPort }}
      {{ end }}
      volumes:
      - type: volume
        source: {{ dig .Init.sk "instanceServiceName" "" .Shared }}-data
        target: /var/lib/postgresql/data
      healthcheck:
        test: ["CMD", "pg_isready", "-U", "postgres"]
        interval: 2s
        timeout: 2s
        retries: 15
    {{ dig .Init.sk "instanceServiceName" "" .Shared }}-init:
      image: mirror.gcr.io/postgres:17-alpine
      entrypoint: ["/bin/sh"]
      environment:
        POSTGRES_PASSWORD: {{ dig .Init.sk "instancePassword" "" .Shared | quote }}
      command:
      - "-c"
      - |
        cd /db-scripts
        ls db-*.sql | xargs cat | psql "postgresql://postgres:$${POSTGRES_PASSWORD}@{{ dig .Init.sk "instanceServiceName" "" .Shared }}:5432/postgres"
      labels:
        dev.score.compose.labels.is-init-container: "true"
      depends_on:
        {{ dig .Init.sk "instanceServiceName" "" .Shared }}:
          condition: service_healthy
          restart: true
      volumes:
      - type: bind
        source: {{ .MountsDirectory }}/{{ dig .Init.sk "instanceServiceName" "" .Shared }}-db-scripts
        target: /db-scripts    
  info_logs: |
    - "{{.Uid}}: To connect to postgres, enter password {{ .State.password | squote }} at: \"docker run -it --network {{ .ComposeProjectName }}_default --rm postgres:17-alpine psql -h {{ dig .Init.sk "instanceServiceName" "" .Shared }} -U {{ .State.username }} --dbname {{ .State.database }}\""
    {{ if ne .Init.publishPort "0" }}
    - "{{.Uid}}: Or connect your postgres client to \"postgres://{{ .State.username }}:{{ .State.password }}@localhost:{{ .Init.publishPort }}/{{ .State.database }}\""
    {{ end }}    
  expected_outputs:
    - host
    - port
    - name
    - database
    - username
    - password