# Blockscout Database Credentials

## Blockscout Database Configuration

**VMID 5000 (Blockscout Container)**

### Database Credentials
- **User**: `blockscout`
- **Password**: `blockscout`
- **Database**: `blockscout`
- **Host**: `postgres` (Docker service name) or `localhost` (from host)
- **Port**: `5432`

### Verification

```bash
# From inside VMID 5000
docker exec -it blockscout-postgres env | grep POSTGRES
```

**Output:**
```
POSTGRES_USER=blockscout
POSTGRES_PASSWORD=blockscout
POSTGRES_DB=blockscout
```

---

## Important Distinction

### Two Separate Databases

1. **Blockscout Database** (VMID 5000)
   - User: `blockscout`
   - Database: `blockscout`
   - Password: `blockscout`
   - Used by: Blockscout explorer application

2. **Explorer Backend Database** (Separate)
   - User: `explorer`
   - Database: `explorer`
   - Password: `changeme`
   - Used by: Custom explorer backend API

These are **completely separate databases** and should not be confused.

---

## Blockscout Database Commands

### Connect to Blockscout Database

```bash
# From VMID 5000
docker exec -it blockscout-postgres psql -U blockscout -d blockscout

# Or from Proxmox host
pct exec 5000 -- docker exec -it blockscout-postgres psql -U blockscout -d blockscout
```

### Run Migrations (Blockscout Database)

```bash
# From VMID 5000
BLOCKSCOUT_CONTAINER=$(docker ps -a | grep blockscout | grep -v postgres | awk '{print $1}' | head -1)

# Run migrations for Blockscout database
docker exec -it $BLOCKSCOUT_CONTAINER bin/blockscout eval "Explorer.Release.migrate()"
```

### Check Tables in Blockscout Database

```bash
# List all tables
docker exec -it blockscout-postgres psql -U blockscout -d blockscout -c "\dt"

# Check specific tables
docker exec -it blockscout-postgres psql -U blockscout -d blockscout -c "
SELECT table_name 
FROM information_schema.tables 
WHERE table_schema = 'public' 
ORDER BY table_name;
"

# Check if critical tables exist
docker exec -it blockscout-postgres psql -U blockscout -d blockscout -c "
SELECT 
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'blocks') 
    THEN '✅ blocks' ELSE '❌ blocks' END,
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'transactions') 
    THEN '✅ transactions' ELSE '❌ transactions' END,
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'migrations_status') 
    THEN '✅ migrations_status' ELSE '❌ migrations_status' END;
"
```

### Reset Blockscout Database Password (if needed)

```bash
# Connect as postgres superuser (if accessible)
docker exec -it blockscout-postgres psql -U postgres << EOF
ALTER USER blockscout WITH PASSWORD 'blockscout';
EOF
```

---

## Explorer Backend Database (Separate)

The explorer backend API uses a **different database**:

- **User**: `explorer`
- **Database**: `explorer`
- **Password**: `changeme`

See `docs/DATABASE_PASSWORD_FIX.md` for explorer backend database fixes.

---

## Connection Strings

### Blockscout Database Connection String

```bash
# From Blockscout container
DATABASE_URL=postgresql://blockscout:blockscout@postgres:5432/blockscout

# From host (if postgres port is exposed)
DATABASE_URL=postgresql://blockscout:blockscout@localhost:5432/blockscout
```

### Explorer Backend Database Connection String

```bash
# From explorer backend
DATABASE_URL=postgresql://explorer:changeme@localhost:5432/explorer
```

---

## Troubleshooting

### Blockscout Can't Connect to Database

```bash
# Check if postgres container is running
docker ps | grep postgres

# Check database connectivity from Blockscout container
docker exec -it blockscout ping -c 3 postgres

# Test database connection
docker exec -it blockscout-postgres psql -U blockscout -d blockscout -c "SELECT 1;"
```

### Verify Database Credentials

```bash
# Check environment variables in postgres container
docker exec -it blockscout-postgres env | grep POSTGRES

# Check Blockscout container environment
docker exec -it blockscout env | grep DATABASE
```

---

## Summary

- **Blockscout Database**: `blockscout` / `blockscout` / `blockscout`
- **Explorer Backend Database**: `explorer` / `explorer` / `changeme`
- These are **two separate databases** serving different purposes
- Blockscout database is managed by Blockscout migrations
- Explorer backend database is managed by the custom backend API

