[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
-
PostgreSQL
You must have an exist PostgreSQL server.
See PostgreSQL: Downloads - https://www.postgresql.org/download/ to learn more.
Install Pulumi and NodeJS
Pulumi
Install the Pulumi - https://www.pulumi.com/ CLI.
1 | Mac OS X |
NodeJS Language Runtime
Install Node.js - https://nodejs.org/en/.
1 | Mac OS X |
Pulumi New
First, create a directory col-pulumi-github-typescript or with your prefer name.
1 | mkdir col-pulumi-github-typescript |
Pulumi login into local file system.
1 | pulumi login file://. |
Pulumi new a project with TypeScript SDK.
1 | pulumi new typescript -f |
The above command will create some files within the current directory.
1 | tree . |
See and modify index.ts file.
1 | # main.ts |
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 | export PGHOST=XXXXXXXXXXXXXX |
Set them using configuration, if you prefer that they be stored alongside your Pulumi stack for easy multi-user access:
1 | pulumi config set postgresql:host XXXXXXXXXXXXXX |
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 | pulumi config set postgresql:password XXXXXXXXXXXXXX --secret |
It will put postgresql:password and value into ./Pulumi.dev.yaml.
1 | Pulumi.dev.yaml |
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 | pulumi destroy |
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/
[4] Pulumi - Modern Infrastructure as Code - https://www.pulumi.com/