Initializer Service
The Initializer service is responsible for preparing the database before other services can run. It verifies the schema, ensures all required components exist, and seeds the database with initial relay URLs.
Overview
Section titled “Overview”| Property | Value |
|---|---|
| Service Name | initializer |
| Lifecycle | One-shot (runs once, then exits) |
| Dependencies | PostgreSQL database |
| Configuration | yaml/services/initializer.yaml |
Operations
Section titled “Operations”The Initializer performs these operations in sequence:
1. Verify Extensions
Section titled “1. Verify Extensions”Checks that required PostgreSQL extensions are installed:
pgcrypto- For cryptographic hash functionsbtree_gin- For GIN index support
SELECT extname FROM pg_extension WHERE extname = 'pgcrypto';2. Verify Tables
Section titled “2. Verify Tables”Confirms all expected tables exist:
relays- Registry of known relay URLsevents- Nostr eventsevents_relays- Event-relay junction tablenip11- Deduplicated NIP-11 documentsnip66- Deduplicated NIP-66 test resultsrelay_metadata- Time-series metadata snapshotsservice_state- Service state persistence
3. Verify Procedures
Section titled “3. Verify Procedures”Confirms all stored procedures exist:
insert_event- Atomic event insertioninsert_relay- Idempotent relay insertioninsert_relay_metadata- Metadata with deduplicationdelete_orphan_events- Cleanup orphaned eventsdelete_orphan_nip11- Cleanup orphaned NIP-11delete_orphan_nip66- Cleanup orphaned NIP-66
4. Verify Views
Section titled “4. Verify Views”Confirms analytics views exist:
relay_metadata_latest- Latest metadata per relay
5. Seed Relays
Section titled “5. Seed Relays”Loads initial relay URLs from the seed file:
# Contains 8,865 relay URLswss://relay.damus.iowss://relay.nostr.bandwss://nos.lol...Configuration
Section titled “Configuration”# Schema verification settingsverify: extensions: true # Verify PostgreSQL extensions tables: true # Verify tables exist procedures: true # Verify stored procedures exist views: true # Verify views exist
# Expected schema elementsschema: extensions: - pgcrypto - btree_gin tables: - relays - events - events_relays - nip11 - nip66 - relay_metadata - service_state procedures: - insert_event - insert_relay - insert_relay_metadata - delete_orphan_events - delete_orphan_nip11 - delete_orphan_nip66 views: - relay_metadata_latest
# Seed relay configurationseed: enabled: true # Enable relay seeding file_path: data/seed_relays.txt # Path to seed fileDocker Compose
Section titled “Docker Compose”The Initializer runs automatically on deployment:
initializer: command: ["python", "-m", "services", "initializer"] restart: no # One-shot, doesn't restart depends_on: pgbouncer: condition: service_healthyManual Run
Section titled “Manual Run”cd implementations/bigbrotrpython -m services initializerWith Custom Config
Section titled “With Custom Config”python -m services initializer --config yaml/services/initializer.yamlOutput
Section titled “Output”Successful initialization produces output like:
INFO initializer: verification_startedINFO initializer: extensions_verified count=2INFO initializer: tables_verified count=7INFO initializer: procedures_verified count=6INFO initializer: views_verified count=1INFO initializer: schema_verified extensions=2 tables=7 procedures=6 views=1INFO initializer: seeding_started file=data/seed_relays.txtINFO initializer: relays_seeded count=8865INFO initializer: initialization_complete duration=2.34Error Handling
Section titled “Error Handling”Missing Extension
Section titled “Missing Extension”ERROR initializer: extension_missing name=pgcryptoSolution: Install the extension in PostgreSQL:
CREATE EXTENSION pgcrypto;Missing Table
Section titled “Missing Table”ERROR initializer: table_missing name=eventsSolution: Run the SQL schema files in postgres/init/ in order.
Seed File Not Found
Section titled “Seed File Not Found”ERROR initializer: seed_file_not_found path=data/seed_relays.txtSolution: Verify the seed file path in configuration and that the file exists.
Seed File Format
Section titled “Seed File Format”The seed file is a plain text file with one relay URL per line:
wss://relay.damus.iowss://relay.nostr.bandwss://nos.lolwss://relay.snort.social# Comments are supportedwss://nostr.wine- One URL per line
- Lines starting with
#are ignored - Empty lines are ignored
- URLs must be valid WebSocket URLs (
wss://orws://)
Customizing Seeds
Section titled “Customizing Seeds”To use custom seed relays:
- Create your seed file:
cat > data/my_relays.txt << EOFwss://my-relay.example.comwss://another-relay.example.comEOF- Update configuration:
seed: enabled: true file_path: data/my_relays.txt- Run initializer:
python -m services initializerIntegration with Docker
Section titled “Integration with Docker”In Docker Compose, the Initializer typically:
- Waits for PostgreSQL to be healthy
- Runs schema verification
- Seeds the database
- Exits with code 0 (success)
Other services wait for Initializer to complete:
finder: depends_on: initializer: condition: service_completed_successfullyNext Steps
Section titled “Next Steps”- Learn about Finder Service
- Explore the Database Schema
- Understand Configuration