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

# Connect to a database

> Connection URLs for every engine, how to add them to a project, and code for Node.js, Python, Go, PHP and Prisma.

Every Pxxl database has a host, a port, a username, a password and a database name. Your app combines them into a connection URL. This page shows where to find them, the URL format for each engine, and working code.

## Find the credentials

Open the database and select **Connection**. It lists:

| Field | Example | Notes |
| - | - | - |
| **Database URL** | `postgresql://pxxluser_ab12:…@k3x9q2.pxxldb.pxxl.pro:41873/pxxldb_cd34` | Hidden until you reveal it. |
| **Host** | `k3x9q2.pxxldb.pxxl.pro` | The database's ID followed by `.pxxldb.pxxl.pro`. |
| **Port** | `41873` | A port between 30000 and 60999, unique to this database. |
| **Username** | `pxxluser_ab12` | Generated when the database is created. |
| **Password** | `••••••••` | Hidden until you reveal it. |

Revealing a secret needs a recent sign-in. If you signed in more than 10 minutes ago, Pxxl asks you to confirm it's you.

From the CLI:

```bash theme={null}
pxxl db get <database-id>
pxxl db credential <database-id> database-url
```

> [!WARNING]
>
> The port is never the engine's default. A URL with `:5432` or `:3306` won't reach a Pxxl database. Copy the port from the **Connection** tab.

## URL format by engine

| Engine | URL |
| - | - |
| PostgreSQL (all images) | `postgresql://USER:PASSWORD@HOST:PORT/DATABASE` |
| MySQL, MariaDB | `mysql://USER:PASSWORD@HOST:PORT/DATABASE` |
| MongoDB | `mongodb://USER:PASSWORD@HOST:PORT/DATABASE?authSource=admin` |
| Redis, KeyDB, DragonflyDB | `redis://default:PASSWORD@HOST:PORT/0` |
| ClickHouse | `clickhouse://USER:PASSWORD@HOST:PORT/DATABASE` (native protocol) |

> [!NOTE]
>
> For MySQL and MariaDB, the dashboard's **Database URL** is written in the Go driver's style, `mysql://USER:PASSWORD@tcp(HOST:PORT)/DATABASE`. Most other clients (Node.js, PHP, Prisma, Python) expect the form in the table. Build it from the host, port, username and password fields.

If your password contains characters such as `@`, `/` or `#`, URL-encode them (`@` becomes `%40`).

## Add it to your project

**1. Add the variable**

In your project, open **Environment variables** and add `DATABASE_URL` with the URL as its value. If you create the database from inside the project, **Add DATABASE\_URL** does this for you.

**2. Redeploy**

Running apps only see new variables after a redeploy or restart.

**3. Read it in your code**

Use `process.env.DATABASE_URL`, `os.environ["DATABASE_URL"]` or your framework's equivalent.

Pxxl only ever sets `DATABASE_URL`, whatever the engine. If your code expects `REDIS_URL` or `MONGODB_URI`, add that variable yourself with the same value.

> [!WARNING]
>
> Don't use `localhost` or `127.0.0.1`. Inside your app's container, `localhost` is the container itself. Use the host Pxxl shows.

## Examples

> [tab] Node.js (pg)
>
> ```js theme={null}
> import pg from "pg";
>
> const pool = new pg.Pool({
>   connectionString: process.env.DATABASE_URL,
>   max: 10, // keep the pool small; every app instance opens its own
> });
>
> const { rows } = await pool.query("select now()");
> console.log(rows[0]);
> ```

> [tab] Prisma
>
> ```prisma theme={null}
> datasource db {
>   provider = "postgresql" // or "mysql", "mongodb"
>   url      = env("DATABASE_URL")
> }
> ```
>
> Run migrations during your build or with a [pre-deploy command](https://docs.pxxl.app/deploy/builds.md):
>
> ```bash theme={null}
> npx prisma migrate deploy
> ```

> [tab] Python
>
> ```python theme={null}
> import os
> import psycopg  # pip install "psycopg[binary]"
>
> with psycopg.connect(os.environ["DATABASE_URL"]) as conn:
>     print(conn.execute("select now()").fetchone())
> ```
>
> For Django, use `dj-database-url`:
>
> ```python theme={null}
> import dj_database_url
> DATABASES = {"default": dj_database_url.config(conn_max_age=60)}
> ```

> [tab] Go
>
> ```go theme={null}
> import (
>     "context"
>     "os"
>
>     "github.com/jackc/pgx/v5/pgxpool"
> )
>
> pool, err := pgxpool.New(context.Background(), os.Getenv("DATABASE_URL"))
> ```
>
> For MySQL with `go-sql-driver/mysql`, drop the `mysql://` prefix from the dashboard URL: `USER:PASSWORD@tcp(HOST:PORT)/DATABASE`.

> [tab] PHP / Laravel
>
> ```bash theme={null}
> DB_CONNECTION=mysql
> DB_HOST=k3x9q2.pxxldb.pxxl.pro
> DB_PORT=41873
> DB_DATABASE=pxxldb_cd34
> DB_USERNAME=pxxluser_ab12
> DB_PASSWORD=your-password
> ```
>
> Laravel also reads `DB_URL` if you prefer a single URL.

> [tab] Redis
>
> ```js theme={null}
> import { createClient } from "redis";
>
> const redis = createClient({ url: process.env.DATABASE_URL });
> await redis.connect();
> await redis.set("hello", "world", { EX: 60 });
> ```

> [tab] MongoDB
>
> ```js theme={null}
> import { MongoClient } from "mongodb";
>
> const client = new MongoClient(process.env.DATABASE_URL);
> await client.connect();
> const orders = client.db().collection("orders");
> ```
>
> Keep `?authSource=admin` in the URL, or sign-in fails.

## Connect from your computer

Use any client: `psql`, TablePlus, DBeaver, MongoDB Compass, `redis-cli`.

```bash theme={null}
psql "postgresql://pxxluser_ab12:PASSWORD@k3x9q2.pxxldb.pxxl.pro:41873/pxxldb_cd34"
mysql -h k3x9q2.pxxldb.pxxl.pro -P 41873 -u pxxluser_ab12 -p pxxldb_cd34
redis-cli -h k3x9q2.pxxldb.pxxl.pro -p 41873 -a PASSWORD
mongosh "mongodb://pxxluser_ab12:PASSWORD@k3x9q2.pxxldb.pxxl.pro:41873/pxxldb_cd34?authSource=admin"
```

If you've set [allowed IPs](https://docs.pxxl.app/databases/access.md#allowed-ips), add your own address first, or the connection is refused.

## Encryption in transit

Connections go through Pxxl's database proxy as plain TCP. The engines don't enforce TLS yet, so treat the password as the main protection: keep it secret, rotate it if it leaks, and restrict access with [allowed IPs](https://docs.pxxl.app/databases/access.md#allowed-ips).

## If it doesn't connect

| Symptom | Likely cause |
| - | - |
| Connection refused or timeout | Wrong port, the database is stopped, or your IP isn't on the allowed list. |
| Password authentication failed | The password was reset, or special characters in it aren't URL-encoded. |
| Works locally, fails on Pxxl | The app still has an old `DATABASE_URL`. Redeploy after changing it. |
| Too many connections | Each app instance opens its own pool. Lower the pool size or scale the database. |

More in [database troubleshooting](https://docs.pxxl.app/troubleshooting/databases.md).


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