Connection URLs¶
Complete reference for database connection URL formats.
URL Format¶
All database URLs follow this general structure:
PostgreSQL¶
Basic Format¶
Examples¶
Local default:
With credentials:
Remote host:
With SSL:
With connection pool:
SSL Modes¶
| Mode | Description |
|---|---|
disable |
No SSL |
allow |
Try SSL, fall back to non-SSL |
prefer |
Try SSL first (default) |
require |
Require SSL, fail if unavailable |
verify-ca |
Require SSL + verify CA |
verify-full |
Require SSL + verify CA + hostname |
Example:
Common Options¶
| Option | Description | Example |
|---|---|---|
sslmode |
SSL connection mode | sslmode=require |
sslcert |
Client certificate | sslcert=/path/to/cert.pem |
sslkey |
Client key | sslkey=/path/to/key.pem |
sslrootcert |
CA certificate | sslrootcert=/path/to/ca.pem |
connect_timeout |
Connection timeout (seconds) | connect_timeout=10 |
application_name |
App name in pg_stat_activity | application_name=myapp |
Cloud Providers¶
AWS RDS:
database_url_sync="postgresql://user:pass@mydb.abc123.us-east-1.rds.amazonaws.com:5432/myapp?sslmode=require"
Google Cloud SQL:
Azure Database:
database_url_sync="postgresql://user@server:pass@server.postgres.database.azure.com:5432/myapp?sslmode=require"
Heroku:
SQLite¶
Basic Format¶
Examples¶
Relative path:
Absolute path:
In-memory (testing only):
In-memory databases are lost when the connection closes. Only use for testing.
Common Options¶
| Option | Description | Example |
|---|---|---|
timeout |
Lock timeout (seconds) | ?timeout=20 |
check_same_thread |
Thread safety check | ?check_same_thread=false |
Example:
MySQL / MariaDB¶
Basic Format¶
Examples¶
Local:
With charset:
With SSL:
Common Options¶
| Option | Description | Example |
|---|---|---|
charset |
Character set | charset=utf8mb4 |
ssl_ca |
CA certificate | ssl_ca=/path/to/ca.pem |
ssl_cert |
Client certificate | ssl_cert=/path/to/cert.pem |
ssl_key |
Client key | ssl_key=/path/to/key.pem |
MariaDB¶
MariaDB uses the same URL format as MySQL:
Configure with database_type="mariadb":
primary = database_config(
database_name="primary",
database_type="mariadb",
database_url_sync="mysql://localhost/myapp",
)
ClickHouse¶
Basic Format¶
Examples¶
Local:
With authentication:
With HTTPS:
Common Options¶
| Option | Description | Example |
|---|---|---|
compression |
Enable compression | compression=1 |
connect_timeout |
Connection timeout | connect_timeout=10 |
send_timeout |
Send timeout | send_timeout=300 |
receive_timeout |
Receive timeout | receive_timeout=300 |
Example:
Environment Variables¶
Basic Pattern¶
import os
primary = database_config(
database_name="primary",
default=True,
database_type="postgresql",
database_url_sync=os.getenv("DATABASE_URL"),
)
With Fallback¶
import os
database_url = os.getenv("DATABASE_URL", "sqlite:///./dev.db")
primary = database_config(
database_name="primary",
default=True,
database_type="postgresql" if "postgresql" in database_url else "sqlite",
database_url_sync=database_url,
)
Required Environment Variables¶
import os
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
raise ValueError("DATABASE_URL environment variable is required")
primary = database_config(
database_name="primary",
default=True,
database_type="postgresql",
database_url_sync=DATABASE_URL,
)
Multiple Databases¶
import os
primary = database_config(
database_name="primary",
default=True,
database_type="postgresql",
database_url_sync=os.getenv("PRIMARY_DATABASE_URL"),
)
analytics = database_config(
database_name="analytics",
database_type="postgresql",
database_url_sync=os.getenv("ANALYTICS_DATABASE_URL"),
)
URL Encoding¶
Special Characters¶
If your password contains special characters, URL-encode them:
| Character | Encoded |
|---|---|
@ |
%40 |
: |
%3A |
/ |
%2F |
? |
%3F |
# |
%23 |
& |
%26 |
% |
%25 |
Example:
Password: p@ss:word
Python URL Encoding¶
from urllib.parse import quote_plus
username = "user"
password = "p@ss:word"
host = "localhost"
database = "myapp"
database_url = f"postgresql://{username}:{quote_plus(password)}@{host}/{database}"
# Result: postgresql://user:p%40ss%3Aword@localhost/myapp
Connection Pools¶
PostgreSQL Pool Options¶
database_url_sync="postgresql://user:pass@localhost/myapp?pool_size=20&max_overflow=10&pool_timeout=30"
| Option | Description | Default |
|---|---|---|
pool_size |
Max connections in pool | 5 |
max_overflow |
Extra connections if pool full | 10 |
pool_timeout |
Wait time for connection (seconds) | 30 |
pool_recycle |
Recycle connections after (seconds) | -1 (never) |
Connection Lifetime¶
Recycle connections after 1 hour:
Testing Connections¶
Verify URL Format¶
from sqlalchemy import create_engine
try:
engine = create_engine("postgresql://user:pass@localhost/myapp")
with engine.connect() as conn:
result = conn.execute("SELECT 1")
print("Connection successful!")
except Exception as e:
print(f"Connection failed: {e}")
Test with DBWarden¶
Common Mistakes¶
Forgetting Port¶
Wrong:
If you need a different port:
Missing Slashes¶
Wrong:
Correct:
database_url_sync="sqlite:///./app.db" # 3 slashes for relative path
database_url_sync="sqlite:////absolute/path/app.db" # 4 slashes for absolute path
Special Characters Not Encoded¶
Wrong:
Correct:
What's Next?¶
- Model Discovery - Configure model paths
- Dev Mode - Local development URLs
- Production Patterns - Real-world examples
- Troubleshooting - Connection issues