# Create and connect to PostgreSQL

Source: https://docs.sliplane.io/databases/getting-started

Managed PostgreSQL databases with backups, SSL, metrics, and access control, managed through Sliplane.

## Create a database

1. In the Sliplane dashboard, go to [**Databases**](https://sliplane.io/app/databases).

2. Click **Create Database**.

3. Choose a **name**, **region**, **compute** size, and **storage** size. See [Pricing](/databases/pricing) for what each option costs.

4. Click **Create Database**.

Your database is ready to use once it finishes provisioning which takes around 30 seconds. Open it to find its connection details.

## Connect to your database

Connect with a PostgreSQL client using `sslmode=verify-full` to validate the TLS certificate and hostname.

For the `psql` and Python examples using `sslrootcert=system`, use libpq 16 or newer with a system CA store.

### Configure the environment

The **Connection URI** contains your connection details:

```txt
postgres://jonas:pAsSworD123@xxxxxx.sliplane.app:1234/mydb?sslmode=verify-full&sslrootcert=system
           ^     ^           ^                   ^    ^
     user -|     |           |- host       port -|    |- database
                 |
                 |- password
```

Copy the Connection URI from the Sliplane dashboard where your database is located and set it as an environment variable `DATABASE_URL`.

**Linux or macOS**

```bash
export DATABASE_URL="postgres://USER:PASSWORD@HOST:PORT/DATABASE?sslmode=verify-full&sslrootcert=system"
```

**Windows Command Prompt**

```bat
set "DATABASE_URL=postgres://USER:PASSWORD@HOST:PORT/DATABASE?sslmode=verify-full&sslrootcert=system"
```

**PowerShell**

```powershell
$Env:DATABASE_URL="postgres://USER:PASSWORD@HOST:PORT/DATABASE?sslmode=verify-full&sslrootcert=system"
```

### Quick starts

Choose a client below:

**psql**

```bash
# Connect to your database
psql "$DATABASE_URL"

# Or run a single query
psql "$DATABASE_URL" -c "SELECT version();"
```

In Windows Command Prompt, use `psql "%DATABASE_URL%"`. In PowerShell, use `psql $Env:DATABASE_URL`.

**JS (pg)**

> **warn**
>
> For `pg`, remove only `sslrootcert=system` from `DATABASE_URL` and keep `sslmode=verify-full`. Otherwise it treats `system` as a certificate filename.

```javascript
import { Client } from "pg"

const client = new Client({ connectionString: process.env.DATABASE_URL })
await client.connect()

const { rows } = await client.query("SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()")
console.log(rows[0]) // { ssl: true, ... }

await client.end()
```

**JS (postgres)**

This example requires `postgres` 3.4.9 or newer.

```javascript
import postgres from "postgres"

const client = postgres(process.env.DATABASE_URL)

const [ ssl ] = await client`SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()`
console.log(ssl) // { ssl: true, ... }

await client.end()
```

**Drizzle**

This example requires `postgres` 3.4.9 or newer.

```javascript
import { drizzle } from "drizzle-orm/postgres-js"
import { sql } from "drizzle-orm"
import postgres from "postgres"

const client = postgres(process.env.DATABASE_URL)
const db = drizzle(client)

const result = await db.execute(sql`SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()`)
console.log(result[0]) // { ssl: true, ... }

await client.end()
```

**Python**

```python
import os
import psycopg

with psycopg.connect(os.environ["DATABASE_URL"]) as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT version()")
        print(cur.fetchone())
```

**Go**

```go
package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/jackc/pgx/v5"
)

func main() {
    ctx := context.Background()

    conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
    if err != nil {
        log.Fatalf("Connect to PostgreSQL: %v", err)
    }
    defer conn.Close(ctx)

    var version string
    if err := conn.QueryRow(ctx, "SELECT version()").Scan(&version); err != nil {
        log.Fatalf("Read PostgreSQL version: %v", err)
    }
    fmt.Println(version)
}
```

**Java**

> **warn**
>
> JDBC URL is different from the common PostgreSQL URI format. To connect, you need to construct the connection string with the host, port, database, user, and password separately, and set `sslmode` to `verify-full` together with `sslfactory` set to `org.postgresql.ssl.DefaultJavaSSLFactory` so the driver validates our certificate against the JVM's trusted CAs.

**Linux or macOS**

```bash
export PGHOST="YOUR_DATABASE_HOST"
export PGPORT="YOUR_DATABASE_PORT"
export PGDATABASE="DATABASE_NAME"
export PGUSER="USERNAME"
export PGPASSWORD="PASSWORD"
```

**Windows Command Prompt**

```bat
set "PGHOST=YOUR_DATABASE_HOST"
set "PGPORT=YOUR_DATABASE_PORT"
set "PGDATABASE=DATABASE_NAME"
set "PGUSER=USERNAME"
set "PGPASSWORD=PASSWORD"
```

**PowerShell**

```powershell
$Env:PGHOST="YOUR_DATABASE_HOST"
$Env:PGPORT="YOUR_DATABASE_PORT"
$Env:PGDATABASE="DATABASE_NAME"
$Env:PGUSER="USERNAME"
$Env:PGPASSWORD="PASSWORD"
```

```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class HelloPostgres {
    public static void main(String[] args) throws Exception {
        String url = String.format(
            "jdbc:postgresql://%s:%s/%s",
            System.getenv("PGHOST"),
            System.getenv("PGPORT"),
            System.getenv("PGDATABASE"));

        Properties props = new Properties();
        props.setProperty("user", System.getenv("PGUSER"));
        props.setProperty("password", System.getenv("PGPASSWORD"));
        props.setProperty("sslmode", "verify-full");
        props.setProperty("sslfactory", "org.postgresql.ssl.DefaultJavaSSLFactory");

        try (Connection conn = DriverManager.getConnection(url, props);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid();")) {
            if (rs.next()) {
                System.out.println(rs.getString("ssl")); // "t"
            }
        }
    }
}
```

#### Detailed guides

The following table lists the official documentation for popular PostgreSQL drivers and ORMs.

| Language or framework   | Library                                                                                                     |
| ----------------------- | ----------------------------------------------------------------------------------------------------------- |
| JavaScript / TypeScript | [node-postgres (`pg`)](https://node-postgres.com/)                                                          |
| JavaScript / TypeScript | [postgres.js](https://github.com/porsager/postgres#installation)                                            |
| JavaScript / TypeScript | [Drizzle ORM](https://orm.drizzle.team/docs/get-started/postgresql-new/)                                    |
| JavaScript / TypeScript | [Prisma ORM](https://www.prisma.io/docs/orm#how-it-works)                                                   |
| Python                  | [psycopg](https://www.psycopg.org/psycopg3/docs/)                                                           |
| Go                      | [pgx](https://github.com/jackc/pgx)                                                                         |
| Java                    | [PostgreSQL JDBC Driver](https://jdbc.postgresql.org/documentation/)                                        |
| .NET                    | [Npgsql](https://www.npgsql.org/doc/index.html)                                                             |
| Ruby                    | [`pg` gem](https://rubygems.org/gems/pg)                                                                    |
| PHP                     | [PDO\_PGSQL](https://www.php.net/manual/en/ref.pdo-pgsql.php)                                               |
| Rust                    | [sqlx / tokio-postgres](https://docs.rs/sqlx/latest/sqlx/)                                                  |
| Laravel                 | [Eloquent PostgreSQL driver](https://laravel.com/docs/database#configuration)                               |
| Django                  | [`django.db.backends.postgresql`](https://docs.djangoproject.com/en/stable/ref/databases/#postgresql-notes) |

### Connect with a GUI

Prefer a graphical client? Follow one of these quickstarts to connect with your database's connection details.

[pgAdmin](/databases/gui/pgadmin)

Connect to your database with pgAdmin, the official PostgreSQL admin tool.

[CloudBeaver](/databases/gui/cloudbeaver)

Connect to your database with CloudBeaver, a browser-based database client.

[n8n](/databases/gui/n8n)

Connect to your database with n8n, the open-source workflow automation tool.

[Metabase](/databases/gui/metabase)

Connect to your database with Metabase, an open-source business intelligence tool.
