# Databases

Connect directly to authorized databases from a beam.

Use a native database client inside a beam to connect to a database enrolled in Teleport. The database stays on its private network.

## Connect through the virtual network

A Teleport agent running the Database Service can reach the database and dials out to your Teleport cluster. The outbound tunnel makes the database reachable from the Beams runtime without opening an inbound port on your database network.

Inside the beam, connect to the database's VNet hostname. The virtual network routes the connection through Teleport to the Database Service, which connects to the database. The beam's delegated roles determine which databases and database users it can access.

<!-- illustration
id: beam-database-connectivity
style: docs-isometric
type: static
title: Private database access from a beam
description: The Teleport agent dials out. The database client connects through VNet.

Show a database client in the Beams runtime and group the Teleport agent (Database Service) and database inside the private network. Use a dashed arrow from the agent toward the runtime, labelled “Outbound tunnel via Teleport”. Use a separate solid arrow from the runtime toward the agent, labelled “Database connection via VNet”, continuing with a solid arrow from the agent to the database. The dashed and solid arrows show who initiates each connection, not two physical transports. Do not show a direct runtime-to-database connection or an inbound public port. Keep both directions legible on mobile.
-->

## Connect to PostgreSQL

Run these commands inside the beam. Make sure `psql` (the PostgreSQL command-line client) is installed there; `psql --version` checks it. You also need an enrolled PostgreSQL database and a delegated role that allows access as the database user you intend to use.

**Inside the beam**

```sh
tsh db ls --format=json
```

Find the entry for your database and copy its `status.vnet_dns_name`. This DNS label can differ from the Teleport resource name. If the database isn't listed, check the beam's delegated roles before continuing.

Replace `example-postgres` below with that DNS label, `readonly` with your allowed PostgreSQL user, and `postgres` with the database name. `$TELEPORT_CLUSTER` is already set inside the beam. Use `sslmode=disable` because the local VNet connection accepts cleartext and Teleport secures the tunnel.

**Inside the beam**

```sh
psql -w "host=example-postgres.db.$TELEPORT_CLUSTER port=5432 user=readonly dbname=postgres sslmode=disable" \
  -v ON_ERROR_STOP=1 -c 'SELECT current_user, current_database();'
```

Confirm the returned `current_user` and `current_database` match the user and database you intended to connect to. The `-w` flag prevents a password prompt; VNet uses the beam's Teleport identity.

## Get started

- [PostgreSQL guide](/docs/guides/postgres/): Run a demo PostgreSQL database and connect to it from a beam with read-only access.
- [Enroll other databases in Teleport](https://goteleport.com/docs/enroll-resources/database-access/enrollment/): Choose the enrollment instructions for your database engine and environment.
