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

# Connecting to Your Database

> Connect to your Nearbase PostgreSQL database using any client or driver

## Connection String

Every Nearbase instance has a unique connection string available on the **Overview** tab in the [console](https://console.nearbase.dev). It follows this format:

```
postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require
```

<Note>
  SSL is required for all connections. Always include `sslmode=require` (or its
  equivalent for your driver). Connections without SSL are rejected.
</Note>

## Connection Details

| Parameter | Value                           |
| --------- | ------------------------------- |
| Host      | `{instance-id}.pg.nearbase.app` |
| Port      | `5432`                          |
| Database  | `postgres`                      |
| Username  | `admin`                         |
| SSL mode  | `require`                       |

## Code Examples

<CodeGroup>
  ```bash psql theme={null}
  psql "postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require"
  ```

  ```javascript Node.js (pg) theme={null}
  import pg from "pg";

  const pool = new pg.Pool({
    connectionString:
      "postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require",
  });

  const result = await pool.query("SELECT NOW()");
  console.log(result.rows[0]);
  ```

  ```javascript Drizzle ORM theme={null}
  import { drizzle } from "drizzle-orm/node-postgres";
  import { Pool } from "pg";

  const pool = new Pool({
    connectionString:
      "postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require",
  });

  export const db = drizzle(pool);
  ```

  ```typescript Prisma theme={null}
  // Add to your .env file:
  // DATABASE_URL="postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require"

  // schema.prisma
  datasource db {
    provider = "postgresql"
    url      = env("DATABASE_URL")
  }
  ```

  ```python Python (psycopg2) theme={null}
  import psycopg2

  conn = psycopg2.connect(
      "postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require"
  )

  cursor = conn.cursor()
  cursor.execute("SELECT NOW()")
  print(cursor.fetchone())
  conn.close()
  ```

  ```python SQLAlchemy theme={null}
  from sqlalchemy import create_engine, text

  engine = create_engine(
      "postgresql+psycopg2://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require"
  )

  with engine.connect() as conn:
      result = conn.execute(text("SELECT NOW()"))
      print(result.fetchone())
  ```

  ```go Go (pgx) theme={null}
  package main

  import (
      "context"
      "fmt"
      "github.com/jackc/pgx/v5"
  )

  func main() {
      conn, err := pgx.Connect(
          context.Background(),
          "postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require",
      )
      if err != nil {
          panic(err)
      }
      defer conn.Close(context.Background())

      var now string
      conn.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
      fmt.Println(now)
  }
  ```
</CodeGroup>

## Connection Pooling

For production applications, use a connection pool to efficiently reuse database connections rather than opening a new one per request.

<Tip>
  A good starting point: set your pool size to **5–10 connections per CPU** on
  your database instance. You can monitor live active connection counts on the
  [Performance tab](/instances/monitoring).
</Tip>

<CodeGroup>
  ```javascript Node.js (pg) theme={null}
  const pool = new pg.Pool({
    connectionString:
      "postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require",
    max: 10,           // maximum pool size
    idleTimeoutMillis: 30000,
    connectionTimeoutMillis: 2000,
  });
  ```

  ```python SQLAlchemy theme={null}
  engine = create_engine(
      "postgresql+psycopg2://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require",
      pool_size=5,
      max_overflow=10,
      pool_pre_ping=True,
  )
  ```

  ```go Go (pgxpool) theme={null}
  import "github.com/jackc/pgx/v5/pgxpool"

  config, _ := pgxpool.ParseConfig(
      "postgresql://admin:{password}@{instance-id}.pg.nearbase.app:5432/postgres?sslmode=require",
  )
  config.MaxConns = 10

  pool, err := pgxpool.NewWithConfig(context.Background(), config)
  ```
</CodeGroup>

## SSL / TLS

All Nearbase databases require TLS. If your driver accepts a configuration object instead of a connection string, enable SSL explicitly:

```javascript theme={null}
const pool = new pg.Pool({
  host: "{instance-id}.pg.nearbase.app",
  port: 5432,
  database: "postgres",
  user: "admin",
  password: "{password}",
  ssl: { rejectUnauthorized: true },
});
```

Need help? Reach out at [team@m.nearbase.dev](mailto:team@m.nearbase.dev).
