Database Services
Slipway makes it easy to provision and manage databases for your Sails applications. No more manual Docker commands or connection string juggling.
Supported Databases
| Database | Type | Use Case |
|---|---|---|
| PostgreSQL | Relational | Primary database for most apps |
| MySQL | Relational | Alternative relational database |
| Redis | Key-Value | Caching, sessions, queues |
| MongoDB | Document | Document-based storage |
Creating a Database
Via CLI
# Interactive (prompts for type)
slipway db:create mydb
# Explicit type
slipway db:create mydb --type=postgres
slipway db:create cache --type=redis
slipway db:create docs --type=mongodbVia Dashboard
- Go to your project and select an environment
- In the Services section, click + Add service (or use the dropdown menu and select Create service)
- Select the database type
- Configure name and version
- Click Create
Linking Databases to Apps
The magic of Slipway's service linking: automatic environment variable injection.
How It Works
When you link a database to an app, Slipway automatically sets the appropriate connection environment variable:
slipway db:link mydb myappThis automatically sets:
| Database Type | Environment Variable |
|---|---|
| PostgreSQL | DATABASE_URL=postgres://user:pass@host:5432/mydb |
| MySQL | DATABASE_URL=mysql://user:pass@host:3306/mydb |
| Redis | REDIS_URL=redis://host:6379 |
| MongoDB | DATABASE_URL=mongodb://user:pass@host:27017/mydb |
Sails Configuration
Your Sails app can read these directly in config/datastores.js:
// config/datastores.js
module.exports.datastores = {
default: {
// Slipway sets DATABASE_URL automatically when you link a database
url: process.env.DATABASE_URL
}
}For Redis (sessions, caching):
// config/session.js
module.exports.session = {
adapter: '@sailshq/connect-redis',
url: process.env.REDIS_URL
}Unlinking
To remove a database link:
slipway db:unlink mydb myappThis removes the environment variable. The database itself is not deleted.
Connecting Directly
Need to run SQL queries or inspect data? Connect directly to your database:
# PostgreSQL - opens psql
slipway db:connect mydb
# MySQL - opens mysql client
slipway db:connect mysql-db
# Redis - opens redis-cli
slipway db:connect cache
# MongoDB - opens mongosh
slipway db:connect docsExample Session
$ slipway db:connect mydb
Connecting to PostgreSQL database 'mydb'...
psql (16.1)
Type "help" for help.
mydb=> \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+-------
public | user | table | slipway
public | post | table | slipway
public | comment | table | slipway
(3 rows)
mydb=> SELECT COUNT(*) FROM "user";
count
-------
156
(1 row)Database Management
List All Databases
slipway db:listOutput:
NAME TYPE STATUS LINKED TO CREATED
mydb postgres running myapp 2 days ago
cache redis running myapp, api 5 days ago
analytics postgres running - 1 week agoGet Connection URL
Retrieve the connection string for a database:
slipway db:url mydbOutput:
postgres://slipway_abc123:[email protected]:5432/mydbSecurity
Connection strings contain credentials. Don't share them or commit them to version control.
Database Info
Get detailed information about a database:
slipway db:info mydbOutput:
Database: mydb
Type: PostgreSQL 16
Status: running
Container: slipway-db-mydb-abc123
Connection:
Internal Host: mydb.internal
Internal Port: 5432
Database: mydb
Username: slipway_abc123
Linked Apps:
- myapp (DATABASE_URL)
Created: 2024-01-15 10:30:00Backups
Create a Backup
slipway db:backup mydbThis creates a timestamped backup:
Creating backup of 'mydb'...
✓ Backup created: mydb-2024-01-20-143022.sql.gz (2.3 MB)Backup to S3-Compatible Storage
Store backups offsite in S3-compatible storage (R2, Spaces, S3):
1. Configure global storage credentials:
slipway config:set \
R2_ACCESS_KEY=your-access-key \
R2_SECRET_KEY=your-secret-key \
R2_BUCKET=my-backups \
R2_ENDPOINT=https://account-id.r2.cloudflarestorage.com2. Backup directly to S3:
slipway db:backup mydb --to-s3Output:
Creating backup of 'mydb'...
✓ Backup created: mydb-2024-01-20-143022.sql.gz (2.3 MB)
✓ Uploaded to S3: s3://my-backups/slipway/backups/mydb-2024-01-20-143022.sql.gz3. Configure automatic S3 backups:
# Enable automatic daily backups to S3
slipway db:update mydb --backup-to-s3=true --backup-schedule="0 3 * * *"Backup Schedule Format
The schedule uses cron format: minute hour day month weekday
0 3 * * *- Daily at 3 AM0 3 * * 0- Weekly on Sundays at 3 AM0 */6 * * *- Every 6 hours
List Backups
slipway db:backups mydbOutput:
BACKUP SIZE LOCATION CREATED
mydb-2024-01-20-143022.sql.gz 2.3 MB s3 2 hours ago
mydb-2024-01-19-100000.sql.gz 2.1 MB s3 1 day ago
mydb-2024-01-18-100000.sql.gz 2.0 MB local 2 days agoRestore from Backup
# Restore from local backup
slipway db:restore mydb mydb-2024-01-20-143022.sql.gz
# Restore from S3 backup
slipway db:restore mydb mydb-2024-01-20-143022.sql.gz --from-s3Destructive Operation
Restoring a backup will replace all data in the database. Make sure you have a recent backup before restoring.
Download Backup
Download a backup to your local machine:
# From local storage
slipway db:backup:download mydb mydb-2024-01-20-143022.sql.gz
# From S3
slipway db:backup:download mydb mydb-2024-01-20-143022.sql.gz --from-s3Backup Retention
Configure how long to keep backups:
# Keep backups for 30 days
slipway db:update mydb --backup-retention=30d
# Keep last 10 backups
slipway db:update mydb --backup-retention=10Database Versions
Supported Versions
| Database | Versions |
|---|---|
| PostgreSQL | 16 (default), 15, 14, 13 |
| MySQL | 8.0 (default), 5.7 |
| Redis | 7 (default), 6 |
| MongoDB | 7 (default), 6, 5 |
Specifying Version
slipway db:create mydb --type=postgres --version=15Connection Pooling
For high-traffic applications, Slipway automatically configures connection pooling:
- PostgreSQL: PgBouncer (optional)
- MySQL: ProxySQL (optional)
Enable pooling when creating a database:
slipway db:create mydb --type=postgres --poolingBest Practices
1. Use Meaningful Names
# Good - descriptive names
slipway db:create myapp-production --type=postgres
slipway db:create myapp-cache --type=redis
# Avoid - generic names
slipway db:create db1 --type=postgres2. Separate Databases per Environment
# Production
slipway db:create myapp-prod-db --type=postgres
slipway db:link myapp-prod-db myapp --env=production
# Staging
slipway db:create myapp-staging-db --type=postgres
slipway db:link myapp-staging-db myapp --env=staging3. Regular Backups
Set up automated backups (coming soon) or run manual backups before major changes:
# Before a migration
slipway db:backup mydb
slipway slide --message "Run migrations"4. Use Redis for Sessions in Production
# Create Redis for sessions
slipway db:create sessions --type=redis
slipway db:link sessions myappThen configure Sails:
// config/env/production.js
module.exports = {
session: {
adapter: '@sailshq/connect-redis',
url: process.env.REDIS_URL
}
}Troubleshooting
Connection Refused
If your app can't connect to the database:
Verify the database is running:
bashslipway db:info mydbCheck if the database is linked:
bashslipway db:listVerify environment variables:
bashslipway env:list myapp
Database Won't Start
Check the database logs:
slipway db:logs mydbCommon issues:
- Out of disk space: Free up space or resize your server
- Out of memory: Reduce
max_connectionsor upgrade server - Corrupted data: Restore from backup
Slow Queries
Connect to the database:
bashslipway db:connect mydbFor PostgreSQL, check slow queries:
sqlSELECT query, calls, mean_time FROM pg_stat_statements ORDER BY mean_time DESC LIMIT 10;
What's Next?
- Learn about Helm for querying your database via the Sails REPL
- Set up Environment Variables for other configuration
- Configure Auto-Deploy for continuous deployment