[Pulumi PostgreSQL] Use Pulumi PostgreSQL provider TypeScript SDK to manage PostgreSQL

Pulumi is a Modern Infrastructure as Code (IaC) to create, deploy, and manage infrastructure on any cloud using familiar programming languages and tools.

The Pulumi PostgreSQL provider uses the PostgreSQL SDK to manage and provision resources.

This article is about how to use Pulumi PostgreSQL provider and TypeScript SDK to manage PostgreSQL.

Prerequisites

Install Pulumi and NodeJS

Pulumi

Install the Pulumi - https://www.pulumi.com/ CLI.

1
2
# Mac OS X
$ brew install pulumi

NodeJS Language Runtime

Install Node.js - https://nodejs.org/en/.

1
2
# Mac OS X
$ brew install node

Pulumi New

First, create a directory col-pulumi-github-typescript or with your prefer name.

1
2
$ mkdir col-pulumi-github-typescript
$ cd col-pulumi-github-typescript

Pulumi login into local file system.

1
2
3
$ pulumi login file://.
Logged in to cloudolife as cloudolife (file://.)
or visit https://pulumi.com/docs/reference/install/ for manual instructions and release notes.

Pulumi new a project with TypeScript SDK.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ pulumi new typescript -f
This command will walk you through creating a new Pulumi project.

Enter a value or leave blank to accept the (default), and press <ENTER>.
Press ^C at any time to quit.

project name: (col-pulumi-postgresql-typescript)
project description: (A minimal TypeScript Pulumi program)
Created project 'col-pulumi-postgresql-typescript'

stack name: (dev)
Created stack 'dev'
Enter your passphrase to protect config/secrets:
Re-enter your passphrase to confirm:

Enter your passphrase to unlock config/secrets
(set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember):
Installing dependencies...

...

Your new project is ready to go! ✨

To perform an initial deployment, run 'pulumi up'

The above command will create some files within the current directory.

1
2
3
4
5
6
7
8
9
10
$ tree .
.
├── Pulumi.dev.yaml
├── Pulumi.yaml
├── README.md
├── index.ts
├── node_modules
├── package-lock.json
├── package.json
└── tsconfig.json

See and modify index.ts file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# main.ts

import * as postgresql from "@pulumi/postgresql";

const roleName = "my-role"
const role = new Role(roleName, {
"login": true,
"name": roleName,
"password": "password",
})

const databaseName = "my-database"
const myDb = new postgresql.Database(databaseName, {
"name": databaseName,
"owner": role.name,
})

NPM Install

Then, install pulumi-postgresql TypeScript SDK.

1
$ npm install @pulumi/postgresql

Pulumi Configuration

The Pulumi PostgreSQL Provider needs to be configured with PostgreSQL credentials before it can be used to manage resources.

Configuring Credentials

In order to communicate your configuration details to Pulumi:

Set the environment variables PGHOST and PGUSER:

1
2
3
$ export PGHOST=XXXXXXXXXXXXXX

$ export PGUSER=YYYYYYYYYYYYYY

Set them using configuration, if you prefer that they be stored alongside your Pulumi stack for easy multi-user access:

1
2
3
$ pulumi config set postgresql:host XXXXXXXXXXXXXX

$ pulumi config set postgresql:username YYYYYYYYYYYYYY

If you are going to set postgresql:password, please remember to pass --secret so that it is properly encrypted. A full set of configuration parameters can be found listed on the Project README.

1
2
3
4
pulumi config set postgresql:password XXXXXXXXXXXXXX --secret
value:
Enter your passphrase to unlock config/secrets
(set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember):

It will put postgresql:password and value into ./Pulumi.dev.yaml.

1
2
3
4
5
6
# Pulumi.dev.yaml

encryptionsalt: < Encryptionsalt >
config:
postgresql:password:
secure: < Secure >

See Configuration | pulumi/pulumi-postgresql: A Postgresql Pulumi resource package - https://github.com/pulumi/pulumi-postgresql#configuration to learn more configurations.

Pulumi up

Create Repository

1
$ pulumi up

It will create a PostgreSQL role and database.

Pulumi Destroy

Destroy all resources created by Pulumi.

1
2
3
$ pulumi destroy
...
If you want to remove the stack completely, run 'pulumi stack rm dev'.

References

[1] PostgreSQL | Pulumi - https://www.pulumi.com/docs/intro/cloud-providers/postgresql/

[2] PostgreSQL Setup | Pulumi - https://www.pulumi.com/docs/intro/cloud-providers/postgresql/setup/

[3] pulumi/pulumi-postgresql: A Postgresql Pulumi resource package - https://github.com/pulumi/pulumi-postgresql

[4] Pulumi - Modern Infrastructure as Code - https://www.pulumi.com/

[5] Node.js - https://nodejs.org/en/