| name | database |
| description | Create and manage Replit's built-in PostgreSQL databases, check status, execute SQL queries with safety checks, and run read-only queries against the production database. Use when the user wants to check prod data, debug database issues in production, or asks to "check the prod db", "query production", "look at live data", or "see what's in the database on the deployed app". |
Database Skill
Manage PostgreSQL databases and execute SQL queries safely in your development and production environments.
When to Use
Use this skill when:
- Creating a new PostgreSQL database for your project
- Checking if a database is provisioned and accessible
- Running SQL queries against the development or production database
- Querying data warehouses (BigQuery, Databricks, Snowflake)
When NOT to Use
- Schema migrations in production environments
- Direct modifications to Stripe tables (use Stripe API instead)
- Converting a pre-existing database over to Replit, unless a user explicitly asks you to.
Available Functions
checkDatabase()
Check if the PostgreSQL database is provisioned and accessible.
Parameters: None
Returns: Dict with provisioned (bool) and message (str)
Example:
const status = await checkDatabase();
if (status.provisioned) {
console.log("Database is ready!");
} else {
console.log(status.message);
}
createDatabase()
Create or verify a PostgreSQL database exists for the project.
Parameters: None
Returns: Dict with:
success (bool): Whether operation succeeded
message (str): Status message
alreadyExisted (bool): True if database already existed
secretKeys (list): Environment variables set (DATABASE_URL, PGHOST, etc.)
Example:
const result = await createDatabase();
if (result.success) {
console.log(`Database ready! Environment variables: ${result.secretKeys}`);
}
executeSql()
Execute a SQL query with safety checks.
Parameters:
sqlQuery (str, required): The SQL query to execute
target (str, default "replit_database"): Target database: "replit_database", "bigquery", "databricks", or "snowflake"
environment (str, default "development"): "development" runs against the development database (all SQL operations supported). "production" runs READ-ONLY queries against a replica of the production database (only SELECT queries allowed). Production is only supported for the "replit_database" target. "production" database, depending on when the user last deployed, may have outdated schemas.
sampleSize (int, optional): Sample size for warehouse queries (only for bigquery/databricks/snowflake)
Returns: Dict with:
success (bool): Whether query succeeded
output (str): Query output/results
exitCode (int): Exit code (0 = success)
exitReason (str | None): Reason for exit if failed
Example:
const result = await executeSql({ sqlQuery: "SELECT * FROM users LIMIT 5" });
if (result.success) {
console.log(result.output);
}
const result2 = await executeSql({
sqlQuery: `
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2)
)
`
});
const result3 = await executeSql({
sqlQuery: `
INSERT INTO products (name, price)
VALUES ('Widget', 19.99)
`
});
const result4 = await executeSql({
sqlQuery: "SELECT * FROM users WHERE active = true",
environment: "production"
});
const result5 = await executeSql({
sqlQuery: "SELECT * FROM sales_data WHERE year = 2024",
target: "bigquery",
sampleSize: 100
});
Safety Features
- Environment Isolation: Development queries run against the development database; production queries are READ-ONLY against a read replica
- Stripe Protection: Mutations to Stripe schema tables (stripe.*) are blocked
- Discussion Mode: Mutating queries are blocked in Planning/Discussion mode
- Destructive Query Protection: DROP, TRUNCATE, etc. are blocked via the skill callback path (use the tool interface directly for destructive operations that require user confirmation)
Best Practices
- Prefer the built-in database: Replit's built-in PostgreSQL database is always preferred over external services like Supabase. It supports rollback and integrates directly with the Replit product. Only use external database services if the user has specific requirements. The
pg package should be installed already.
- Check before creating: Call
checkDatabase() before createDatabase() to avoid unnecessary operations
- Use parameterized queries: Avoid string interpolation for user input
- Test queries first: Run SELECT queries before INSERT/UPDATE/DELETE
- Keep backups: Important data should be backed up before destructive operations
Environment Variables
After creating a database, these environment variables are available:
DATABASE_URL: Full connection string
PGHOST: Database host
PGPORT: Database port (5432)
PGUSER: Database username
PGPASSWORD: Database password
PGDATABASE: Database name
Example Workflow
const status = await checkDatabase();
if (!status.provisioned) {
const createResult = await createDatabase();
if (!createResult.success) {
console.log(`Failed: ${createResult.message}`);
}
}
await executeSql({
sqlQuery: `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
)
`
});
await executeSql({
sqlQuery: `
INSERT INTO users (email)
VALUES ('user@example.com')
`
});
const result = await executeSql({ sqlQuery: "SELECT * FROM users" });
console.log(result.output);
Limitations
- Production queries are READ-ONLY (SELECT only) — INSERT, UPDATE, DELETE, and DDL statements will fail
- Production environment is only supported for the "replit_database" target (not data warehouses)
- Cannot modify Stripe schema tables (read-only)
- Destructive queries (DROP, TRUNCATE, etc.) are blocked via the skill callback path
- Mutating queries blocked in Planning mode
Rollbacks
As stated in the diagnostic skills, the development database support rollbacks. Open that skill for more information.