How to programatically create a postgres database?
raymondhechen
PROOP

a year ago

I'm trying to use the graphql API to dynamically create a postgres database in a project. I see pluginCreate in Postman and PluginCreateInput in the schema, but neither have a way to indicate the PluginType of postgresql. Am I going about create a database the wrong way? Thank you!

Solved

10 Replies

raymondhechen
PROOP

a year ago

I'm realizing that I can probably create a database as a service instead, specifically via Railway's postgres template, but I can't seem to find the template id?


a year ago

Hello,

Plugins have long since been deprecated, you would want to use template and then templateDeployV2

I don't know what language you are using, but here is some Python code that does exactly what you want -

https://github.com/brody192/railway_project_create/blob/main/main.py

Hopefully, it will give you a good enough idea of how it needs to work, feel free to ask any further questions!

Best,
Brody


Status changed to Awaiting User Response Railway about 1 year ago


raymondhechen
PROOP

9 months ago

Hi Brody! I think I broadly understand the approach, but I'm not sure what template specifically is the native postgres template that Railway is using. Overall, my goal is to:

  1. Dynamically create a postgres database service

  2. Retrieve the connection url for the database.

I understand for (1), I'll use templateDeployV2(), but as mentioned, I still don't know what the template Id is. And any idea how I should go about (2)?

Thanks!


Status changed to Awaiting Railway Response Railway 9 months ago


9 months ago

  1. The short code is just postgres as shown in the template's url - https://railway.com/deploy/postgres

    And that is what you would use in code.

  2. You should be able to get the service variables with the variables query.

The GQL playground is a great resource, take a look -

https://railway.com/graphiql


Status changed to Awaiting User Response Railway 9 months ago


raymondhechen
PROOP

9 months ago

Hey Brody!

Here's the current implementation I have

import { gql, GraphQLClient } from 'graphql-request'

const RAILWAY_ENDPOINT = 'https://backboard.railway.com/graphql/v2'
const RAILWAY_POSTGRES_TEMPLATE_ID = 'postgres'

const GET_TEMPLATE_SERIALIZED_CONFIG_QUERY = gql`
  query GetTemplateSerializedConfig($code: String!) {
    template(code: $code) {
      id
      serializedConfig
    }
  }
`

const DEPLOY_TEMPLATE_QUERY = gql`
  mutation templateDeployV2($input: TemplateDeployV2Input!) {
    templateDeployV2(input: $input) {
      __typename
      projectId
      workflowId
    }
  }
`

type GetTemplateSerializedConfigResponse = {
  template: {
    id: string
    serializedConfig: unknown
  }
}

export class RailwayClient {
  private client: GraphQLClient

  constructor() {
    this.client = new GraphQLClient(RAILWAY_ENDPOINT, {
      headers: {
        Authorization: `Bearer ${process.env.RAILWAY_TEAM_API_KEY}`,
        'Content-Type': 'application/json',
      },
    })
  }

  public async getDatabaseTemplateSerializedConfig(): Promise<GetTemplateSerializedConfigResponse> {
    return this.client.request(GET_TEMPLATE_SERIALIZED_CONFIG_QUERY, {
      code: RAILWAY_POSTGRES_TEMPLATE_ID,
    })
  }

  public async createDatabase() {
    const config = await this.getDatabaseTemplateSerializedConfig()
    return this.client.request(DEPLOY_TEMPLATE_QUERY, {
      input: {
        teamId: process.env.RAILWAY_TEAM_ID,
        projectId: process.env.RAILWAY_PROJECT_ID,
        environmentId: process.env.RAILWAY_ENVIRONMENT_ID,
        templateId: RAILWAY_POSTGRES_TEMPLATE_ID,
        serializedConfig: config.template.serializedConfig,
      },
    })
  }
}

Upon running railwayClient.createDatabase(), I get this response:

{ "templateDeployV2": { "__typename": "TemplateDeployPayload", "projectId": "e8ed26a9-50cf-46ad-9ada-66a4b8997794", "workflowId": "deployTemplate/project/e8ed26a9-50cf-46ad-9ada-66a4b8997794/1iG36A" } }

But I don't see any services running in that project. Any idea what I'm doing wrong? Thanks!


Status changed to Awaiting Railway Response Railway 9 months ago


9 months ago

Hello,

I see 3 services running in that project?

https://railway.com/project/e8ed26a9-50cf-46ad-9ada-66a4b8997794


Status changed to Awaiting User Response Railway 9 months ago


brody

Hello,I see 3 services running in that project?https://railway.com/project/e8ed26a9-50cf-46ad-9ada-66a4b8997794

raymondhechen
PROOP

9 months ago

Ah yeah, I figured it out - had to replace templateId in TemplateDeployV2() with the uuid returned from getting the template object!


Status changed to Awaiting Railway Response Railway 9 months ago


9 months ago

Awsome, glad you got it figured out!


Status changed to Awaiting User Response Railway 9 months ago


raymondhechen
PROOP

9 months ago

Any chance you know how I could get the serviceId programmatically from the deployed template? I see the TemplateDeployPayload (from TemplateDeployV2) only has projectId and workflowId? Thank you!!


Status changed to Awaiting Railway Response Railway 9 months ago


9 months ago


Status changed to Awaiting User Response Railway 9 months ago


Railway
BOT

7 months ago

This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!

Status changed to Solved Railway 7 months ago


Loading...