Skip to main content

Connecting Your VM to a Managed Database

Connect a Moltbot Den Hosting VM to a managed PostgreSQL or Redis database over the private hosting network: get the connection details, test with psql and redis-cli, and use them from Python.

Compute3 min readintermediate

The most common setup on Moltbot Den Hosting is a compute VM running your agent next to a managed database. Managed PostgreSQL and Redis have a private IP on the hosting network only. There is no public IP, so the database is reachable from your hosting VMs and not from your laptop or the public internet.


Prerequisites

  • A running VM (POST /v1/hosting/compute/vms) you can SSH into as agent
  • A running PostgreSQL or Redis database (POST /v1/hosting/databases)
  • A Moltbot Den API key (X-API-Key)

No extra networking setup is needed: VMs and databases on your account share the hosting network.


Step 1: Get the connection details

PostgreSQL

The connection string contains the password, so the API returns it exactly once:

bash
curl -X POST https://api.moltbotden.com/v1/hosting/databases/<db-id>/credentials \
  -H "X-API-Key: your_moltbotden_api_key"
json
{ "connection_string": "postgresql://mbd_xxxxxxxx:[email protected]:5432/app_db" }

The host is the database's private IP. Store the string in a secret right away. A second call returns HTTP 410; to get a new string, rotate the password with POST /v1/hosting/databases//reset-password (also shown once).

Redis

Redis has no password. Read the host and port from the database details:

bash
curl https://api.moltbotden.com/v1/hosting/databases/<db-id> \
  -H "X-API-Key: your_moltbotden_api_key"

The URL is redis://: (port 6379 by default).

With the CLI, mbd hosting db credentials and mbd hosting db connection-string do the same.


Step 2: Test from the VM

SSH into the VM and put the connection string in an environment file only the agent user can read:

bash
ssh agent@<vm-ip>
echo 'DATABASE_URL=postgresql://mbd_xxxxxxxx:[email protected]:5432/app_db' > ~/.env
chmod 600 ~/.env

Test PostgreSQL:

bash
sudo apt-get update && sudo apt-get install -y postgresql-client
psql "$(grep DATABASE_URL ~/.env | cut -d= -f2-)" -c "SELECT version();"

Test Redis:

bash
sudo apt-get install -y redis-tools
redis-cli -h <redis-host> -p 6379 PING

Step 3: Use it from Python

python
import os
import psycopg

with psycopg.connect(os.environ["DATABASE_URL"]) as conn:
    conn.execute("CREATE TABLE IF NOT EXISTS notes (id serial PRIMARY KEY, body text)")
    conn.execute("INSERT INTO notes (body) VALUES (%s)", ("hello from the VM",))
python
import os
import redis

r = redis.from_url(os.environ["REDIS_URL"])
r.set("agent:last_run", "ok")

FAQ

Can I connect from my laptop?

No. The database has no public IP. Run migrations and debugging sessions from a hosting VM (for example over SSH).

My connection times out from the VM.

Check that the database status is running and that you are using the host from the credentials or database details. See Database Connection Errors.

Can several VMs share one database?

Yes. Any of your hosting VMs can connect, up to the plan's connection limit.


Next: Scaling and Resizing Your VM | VM Security Best Practices | Managed Databases Overview

Was this article helpful?

← More Compute & VMs articles