> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Create and Connect a Managed Database

> Managed database on Square Cloud: create a PostgreSQL, MongoDB, MySQL or Redis instance, get its connection string and TLS certificate, and connect from code.

## Introduction

This tutorial takes you from zero to a live, connected database in a few minutes: create the instance from the dashboard, grab its connection string, and connect to it from your own code.

### Prerequisites

* **Square Cloud Account**: Create your account through the [signup page](https://squarecloud.app/en/signup).
* **Active Standard plan or higher**: Managed databases require an **active Standard plan or higher**. Check the [available plans](/en/platform/plans) and choose the one that fits your workload.

## Step 1: Choose an engine

Square Cloud offers four managed database engines, already configured to connect and start using:

| Engine | Version |
| :-: | :-: |
| PostgreSQL | 17 |
| MongoDB | 8 |
| MySQL | 9 |
| Redis | 8 |

This tutorial uses **PostgreSQL**, but the same dashboard flow applies to every engine above.

## Step 2: Create the database

<Steps>
  <Step title="Create a new database instance">
    Go to the [Databases](https://squarecloud.app/en/dashboard/databases) page and click on the "Create Database" button.
  </Step>

  <Step title="Setting up your database instance">
    Select **PostgreSQL** (or the engine you chose in Step 1) and fill in the required information, such as the database name and RAM.
  </Step>

  <Step title="Finish your instance creation">
    Click on the "Create" button and wait for the database to be created.
  </Step>
</Steps>

## Step 3: Get the connection string

Once your database is ready, open it from the [Databases](https://squarecloud.app/en/dashboard/databases) page. The connection details (host, port, username, password and connection URL) are shown in the database dashboard, alongside the TLS certificate you'll need to connect.

<Note>Square Cloud databases only accept SSL/TLS connections. See [how do I connect to my database](/en/services/databases#how-do-i-connect-to-my-database) for how the certificate is used by different clients.</Note>

## Step 4: Connect from your code

With the connection string in hand, connect using your language's PostgreSQL client.

<Tabs>
  <Tab title="Node.js">
    Install the [`pg`](https://www.npmjs.com/package/pg) package and enable ES modules, which the example below uses (`import` and top-level `await`):

    ```bash theme={"system"}
    npm install pg
    npm pkg set type=module
    ```

    Connect and verify with a simple query:

    ```javascript index.js theme={"system"}
    import fs from "node:fs";
    import { Client } from "pg";

    const cert = fs.readFileSync("certificate.pem").toString();

    const client = new Client({
      connectionString: process.env.DATABASE_URL, // postgresql://user:password@host:port/dbname
      ssl: { ca: cert, cert: cert, key: cert },
    });

    await client.connect();
    const result = await client.query("SELECT 1");
    console.log("Connected! Result:", result.rows[0]);
    await client.end();
    ```
  </Tab>

  <Tab title="Python">
    Install [`psycopg2`](https://pypi.org/project/psycopg2-binary/):

    ```bash theme={"system"}
    pip install psycopg2-binary
    ```

    Connect and verify with a simple query:

    ```python main.py theme={"system"}
    import os
    import psycopg2

    conn = psycopg2.connect(
        os.environ["DATABASE_URL"],  # postgresql://user:password@host:port/dbname
        sslmode="verify-ca",
        sslrootcert="certificate.pem",
        sslcert="certificate.pem",
        sslkey="certificate.pem",
    )
    cur = conn.cursor()
    cur.execute("SELECT 1")
    print("Connected! Result:", cur.fetchone())
    cur.close()
    conn.close()
    ```
  </Tab>
</Tabs>

If the query returns a result instead of raising a connection error, your application is talking to the database successfully.

<Warning>Never hardcode the connection string in your source code. Set it as an environment variable (`DATABASE_URL`) in your application's Environment Variables on the dashboard, or with the CLI command `squarecloud app env set "DATABASE_URL=<connection-url>" --app <appID>` (see [environment variables in the CLI](/en/cli-reference/environment-variables#squarecloud-app-env-set)), so credentials never end up in your repository. Restart the application afterwards: it reads environment variables when it starts. See [Environment variables](/en/platform/environment-variables).</Warning>

Keep `certificate.pem` in your application's ZIP, since the code reads it from disk, but out of any public repository.

## Next steps

You now have a managed database running and reachable from your code. From here:

<CardGroup cols={3}>
  <Card title="Deploy an app to use it" icon="server" href="/en/tutorials/how-to-deploy-your-website">
    Host the application that connects to this database on Square Cloud.
  </Card>

  <Card title="Database connection errors" icon="triangle-exclamation" href="/en/platform/troubleshooting/database-connection-errors">
    Fix SSL/TLS, timeout and access errors.
  </Card>

  <Card title="Snapshots" icon="camera" href="/en/cli-reference/snapshots#squarecloud-db-snapshot-create">
    Create and restore database snapshots from the CLI.
  </Card>
</CardGroup>

## Contact us

If you continue facing **technical difficulties**, our **specialized support team** is available to assist you. [**Contact us**](https://squarecloud.app/en/support) and we'll be happy to help you resolve any issue: support quality is a big part of why developers rate Square Cloud [**4.9/5 across 402 reviews**](https://www.trustpilot.com/review/squarecloud.app) on Google and Trustpilot.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.