Skip to main content
Database commands provide direct access to the scrapai database for migrations, queries, statistics, and transferring data between databases.

db migrate

Run Alembic database migrations to update the database schema.

Syntax

Output

Use Cases

  • After git pull: Apply new schema changes
  • After scrapai upgrade: Update to new database structure
  • Initial setup: Run by ./scrapai setup automatically

db current

Show current migration revision.

Syntax

Output

db stats

Show database statistics.

Syntax

Output

Use Cases

  • Monitor database growth
  • Check queue status at a glance
  • Verify data after imports/migrations

db tables

List all tables with row counts.

Syntax

Output

Table Validation

Only scrapai tables are displayed. System tables and non-scrapai tables are skipped:
Table validation prevents SQL injection by using a whitelist of known scrapai table names.

db inspect

Show schema for a specific table.

Syntax

Arguments

table
string
required
Table name. Must be one of: spiders, scraped_items, crawl_queue, spider_rules, spider_settings, alembic_version.

Examples

Output (SQLite)

Output (PostgreSQL)

Invalid Table

db query

Execute SQL queries (SELECT, UPDATE, DELETE only).

Syntax

Arguments

sql
string
required
SQL query to execute.

Options

--format
choice
default:"table"
Output format: table, json, csv.
--yes
flag
Skip confirmation prompt for UPDATE/DELETE.

SELECT Queries

Output (Table Format)

Output (JSON Format)

Output (CSV Format)

UPDATE Queries

Output

DELETE Queries

Output

DELETE operations cannot be undone. Always verify row count before confirming.

Blocked Operations

INSERT, DROP, ALTER, and TRUNCATE are blocked for safety:

Complex Queries

Joins, aggregations, and subqueries are supported:

db transfer

Transfer data from another database.

Syntax

Arguments

source_url
string
required
Source database connection string.

Options

--skip-items
flag
Skip scraped_items table (transfer only spiders and queue).

Workflow

  1. Update DATABASE_URL in .env to your new database
  2. Run migrations on new database: ./scrapai db migrate
  3. Transfer data from old database: ./scrapai db transfer <old_db_url>

Examples

SQLite to PostgreSQL

PostgreSQL to PostgreSQL

Skip Scraped Items

Output

What Gets Transferred

  • Spiders: All spider configurations
  • Spider Rules: URL matching rules
  • Spider Settings: Custom settings
  • Scraped Items: All scraped data (unless --skip-items)
  • Queue: All queue items with status

ID Remapping

Foreign key relationships are preserved. New spider IDs are assigned and referenced data is updated accordingly.

Error Handling

All operations are transactional. If transfer fails, target database is rolled back.

Safety Features

SQL Injection Prevention

  • Table names validated against whitelist
  • All queries use SQLAlchemy parameterized bindings
  • INSERT, DROP, ALTER, TRUNCATE blocked via db query

Confirmation Prompts

UPDATE and DELETE queries show affected row count and require confirmation. Use --yes to skip prompts.

PostgreSQL vs SQLite

PostgreSQL Benefits

  • Atomic queue operations: FOR UPDATE SKIP LOCKED for race-free parallel processing
  • Better performance for large datasets (100k+ items)
  • Concurrent writes from multiple workers
  • Advanced JSONB operators for querying spider configs

SQLite Benefits

  • Zero configuration, no server setup required
  • Single file, easy to backup and copy
  • Sufficient for most use cases (under 100k items)

Switching Databases

Example Queries

Spider Analytics

Queue Analytics

Scraped Items

Troubleshooting

Migration Failed

Check database connection (DATABASE_URL in .env), write permissions, and disk space.

Query Timeout

Transfer Failed

Next Steps

Data Commands

View and export scraped items

Queue Management

Manage batch processing queue