---
title: "Databases & Analytics - Getting started with Terraform"
description: "Learn how to order a Cloud Database or Analytics service using Terraform."
url: https://docs.us.ovhcloud.com/en/public-cloud/data-analytics/getting-started-terraform
lang: en
---
> For AI agents: the complete documentation index is available at https://docs.us.ovhcloud.com/en/llms.txt, the full documentation bundle is available at https://docs.us.ovhcloud.com/en/llms-full.txt.

# Databases & Analytics - Getting started with Terraform

**Learn how to order a Cloud Database or Analytics service using Terraform.**

Cloud Databases and Analytics[\*](#go-further) allow you to focus on building and deploying cloud applications while OVHcloud takes care of the service infrastructure and maintenance.

 

## Requirements

- [Terraform](https://www.terraform.io/) installed
- Access to the [OVHcloud API](https://api.us.ovhcloud.com/) (create your credentials by consulting [this guide](https://docs.us.ovhcloud.com/en/manage-and-operate/api/first-steps.md))
- A [Public Cloud project](https://us.ovhcloud.com/public-cloud/) in your OVHcloud account

 

## Instructions

### Step 1: Gather the OVHcloud required parameters

#### Getting your cluster/API tokens information

The "OVH provider" needs to be configured with a set of credentials:

- an `application_key`
- an `application_secret`
- a `consumer_key`

_Why?_

Because, behind the scenes, the "OVH Terraform provider" is making requests to OVHcloud APIs.

To retrieve this information, please follow our [First Steps with the OVHcloud API](https://docs.us.ovhcloud.com/en/manage-and-operate/api/first-steps.md) tutorial.

Specifically, you have to generate these credentials via the [OVHcloud token generation page](https://us.ovhcloud.com/auth/api/createToken) with the following rights:

- **GET** `/cloud/project/*/database/*`
- **POST** `/cloud/project/*/database/*`
- **PUT** `/cloud/project/*/database/*`
- **DELETE** `/cloud/project/*/database/*`

Once you have generated your tokens, save them - you will need them shortly.

The last needed information is the `service_name`: it is the ID of your Public Cloud project.

_How to get it?_

In the Public Cloud section, you can retrieve your service name ID thanks to the `Copy to clipboard` button.

You will also use this information in Terraform resources definition files.

 

### Step 2: Gather the set of required parameters

To create a new PostgreSQL[®](#go-further) cluster, you will need to specify at least:

- the _engine_ (e.g., "postgresql")
- the _version_ (e.g., "17")
- the _region_ (e.g. "US-EAST-VA")
- the _plan_ (e.g., "business")
- the _flavor_ of the cluster (e.g., "db1-7")

 

### Step 3: Create Terraform files

**Note:**

You may find it useful to have the [Terraform documentation](https://registry.terraform.io/providers/ovh/ovh/latest/docs/resources/cloud_project_database) open while completing these steps.

First, create a `main.tf` file defining the resources that will be created

```bash
terraform {
  required_providers {
    ovh = {
      source  = "ovh/ovh"
      version = ">=2.11.0"
    }
  }
}

provider "ovh" {
  endpoint           = var.ovh.endpoint
  application_key    = var.ovh.application_key
  application_secret = var.ovh.application_secret
  consumer_key       = var.ovh.consumer_key
}

resource "ovh_cloud_project_database" "service" {
  service_name = var.product.project_id
  description  = var.product.name
  engine       = var.product.engine
  version      = var.product.version
  plan         = var.product.plan
  nodes {
    region = var.product.region
  }
  nodes {
    region = var.product region
  }
  nodes {
    region = var.product region
  }
  flavor = var.product.flavor
  ip_restrictions {
    ip = var.product.ip
  }
}

resource "ovh_cloud_project_database_postgresql_user" "dbuser" {
  service_name = ovh_cloud_project_database.service.service_name
  cluster_id   = ovh_cloud_project_database.service.id
  name         = var.access.name
}
```

Then, create a `variables.tf` file defining the variables used in `main.tf`:

**Note:**

This file will set default values that will be overwritten by the `secrets.tfvars` file in the next step.

```bash
variable "ovh" {
  type = map(string)
  default = {
    endpoint           = "ovh-us"
    application_key    = ""
    application_secret = ""
    consumer_key       = ""
  }
}

variable "product" {
  type = map(string)
  default = {
    project_id = ""
    name       = "postgresql-terraform"
    engine.    = ""
    region     = "US-EAST-VA"
    plan       = "business"
    flavor     = "db1-7"
    version    = "17"
    ip         = "0.0.0.0/32"
  }
}

variable "access" {
  type = map(string)
  default = {
    name = "new_user"
  }
}
```

Here, we defined the `ovh-us` endpoint because we want to call the OVHcloud US API.

Then, create a `secrets.tfvars` file containing the required variables values:

```bash
ovh = {
  endpoint           = "ovh-us"
  application_key    = "<application_key>"
  application_secret = "<application_secret>"
  consumer_key       = "<consumer_key>"
}

product = {
  project_id = "<service_name>"
  name       = "postgresql-terraform"
  engine     = "postresql"
  region     = "US-EAST-VA"
  plan       = "business"
  flavor     = "db1-7"
  version    = "17"
  ip         = "<ip_range>"
}

access = {
  name = "new_user"
}
```

**Note:**

Don't forget to replace `<service_name>`, `<application_key>`, `<application_secret>`, `<consumer_key>`, `<ip_range>` with the real data.

Finally, create an `outputs.tf` file defining the resources that will be exported:

```bash
output "cluster_uri" {
  value = ovh_cloud_project_database.service.endpoints.0.uri
}

output "user_name" {
  value = ovh_cloud_project_database_postgresql_user.dbuser.name
}

output "user_password" {
  value     = ovh_cloud_project_database_postgresql_user.dbuser.password
  sensitive = true
}
```

 

### Step 4: Run

Now we need to initialize Terraform, generate a plan, and apply it.

```
terraform init
```

The [init](https://www.terraform.io/cli/commands/init) command will initialize your working directory which contains `.tf` configuration files.

Run it first for any new configuration, or after checking out a configuration from a git repository.

The `init` command will:

- Download and install Terraform providers/plugins
- Initialize backend (if defined)
- Download and install modules (if defined)

Now, we can generate our plan:

```
terraform plan -var-file=secrets.tfvars
```

Thanks to the [plan](https://www.terraform.io/cli/commands/plan) command, we can check what Terraform wants to create, modify, or remove.

The plan is OK for us, so let's [apply](https://www.terraform.io/cli/commands/apply) it:

**Warning:**

**NOTE:** If executing this command with a pre-existing user name, you may receive an error message stating that the user already exists. Despite this error, your database should have successfully been created.

To avoid this error, run the command below with a new user name.

```
terraform apply -var-file=secrets.tfvars -auto-approve
```

Finally, export the user credentials and the URI.

```bash
export PASSWORD=$(terraform output -raw user_password)
export USER=$(terraform output -raw user_name)
export URI=$(terraform output -raw cluster_uri)
```

**Tip:**

**Congratulations!** The PostgreSQL® cluster is created.

 

## How to deploy with other engines

In this guide, we explained how to deploy a PostgreSQL® service but you can find examples for other engines in our GitHub repository by selecting the folder for your engine and the `terrform/hello-world` folder.

 

## Go further

Browse all the [Databases guides](https://docs.us.ovhcloud.com/en/public-cloud/databases/landing-page-databases.md).

Browse the rest of the [OVHcloud US documentation](https://docs.us.ovhcloud.com/en/index.md) for guides on our other products and services.

Learn more about [Managed Databases](https://us.ovhcloud.com/public-cloud/databases/) on the OVHcloud US website.

OVHcloud Managed Databases and Analytics:

- Grafana® is a registered trademark of Grafana Labs and is used with the permission of Grafana Labs. OVH SAS and its subsidiaries are not affiliated with or endorsed by Grafana Labs.
- Kafka® is a registered trademark of The Apache Software Foundation and has been licensed for use by OVHcloud, who has no affiliation with and is not endorsed by The Apache Software Foundation.
- MongoDB® is a registered trademark of MongoDB, Inc.
- MySQL® is a registered trademark of Oracle and/or its affiliates.
- PostgreSQL® is a registered trademark of the PostgreSQL Community Association of Canada.
