Gulshan
Gulshan

Reputation: 83

How to create tables in Postgres RDS using CDK?

I created a Postgres RDS database using the CDK. I read the documentation but didn't find a way to create tables through it.

The language I'm using is TypeScript.

Upvotes: 1

Views: 3750

Answers (4)

urizaf
urizaf

Reputation: 19

generate a lambda that executes after the creation of the db. No need for a custom resource.

Upvotes: 0

miensol
miensol

Reputation: 41678

There's no AWS CDK support for this. However, I've created a package named cloudformation-sql-run that helps with that. You can use it as follows:

const createPosts = new SqlRun(this, 'Create Posts', {
  vpc: vpc,
  connection: SqlRunConnection.fromDatabaseInstance(db),
  up: {
    run: [{
      sql: `CREATE TABLE posts(name varchar)`
    }, {
      sql: `INSERT INTO posts(name) VALUE (:secret)`,
      parameters: {
        secret: SqlSecret.fromSecretsManager(password)
      }
    }],
  },
  down: {
    run: [{
      sql: `DROP TABLE posts`
    }]
  }
});

Upvotes: 0

shwz
shwz

Reputation: 446

This post might help you. You need to use CustomResource

https://stackoverflow.com/a/62331465/6741215

Upvotes: 0

fedonev
fedonev

Reputation: 25739

You cannot create a RDS table using the CDK or CloudFormation.

You can use the AWS SDK client to execute a CREATE TABLE... command against your database. The SDK client can be used locally or in a lambda function, for instance.

Upvotes: 1

Related Questions