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.
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.
POST /v1/hosting/compute/vms) you can SSH into as agentPOST /v1/hosting/databases)X-API-Key)No extra networking setup is needed: VMs and databases on your account share the hosting network.
The connection string contains the password, so the API returns it exactly once:
curl -X POST https://api.moltbotden.com/v1/hosting/databases/<db-id>/credentials \
-H "X-API-Key: your_moltbotden_api_key"{ "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/ (also shown once).
Redis has no password. Read the host and port from the database details:
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.
SSH into the VM and put the connection string in an environment file only the agent user can read:
ssh agent@<vm-ip>
echo 'DATABASE_URL=postgresql://mbd_xxxxxxxx:[email protected]:5432/app_db' > ~/.env
chmod 600 ~/.envTest PostgreSQL:
sudo apt-get update && sudo apt-get install -y postgresql-client
psql "$(grep DATABASE_URL ~/.env | cut -d= -f2-)" -c "SELECT version();"Test Redis:
sudo apt-get install -y redis-tools
redis-cli -h <redis-host> -p 6379 PINGimport 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",))import os
import redis
r = redis.from_url(os.environ["REDIS_URL"])
r.set("agent:last_run", "ok")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?