# PostgreSQL setup for SyncTide

This guide is for a **new PC** that does not already have PostgreSQL configured.

## 1) Install PostgreSQL
1. Download and install PostgreSQL for Windows.
2. During setup:
   - remember the **postgres** password
   - keep the default port `5432` unless you need a different one
3. Make sure `psql` is available. It is usually installed with PostgreSQL.

## 2) Create the database
Open **SQL Shell (psql)** or PowerShell/CMD and run:

```sql
CREATE DATABASE synctide;
```

Or from a command line:

```bash
psql -U postgres -c "CREATE DATABASE synctide;"
```

## 3) Configure the app
In the deployable folder:
1. copy `.env.example` to `.env` if the installer has not already created it
2. set the PostgreSQL values, for example:

```env
SYNCTIDE_DB_DRIVER=postgresql+psycopg2
SYNCTIDE_DB_USER=postgres
SYNCTIDE_DB_PASSWORD=your_password
SYNCTIDE_DB_HOST=localhost
SYNCTIDE_DB_PORT=5432
SYNCTIDE_DB_NAME=synctide
```

## 4) Create or update the schema
### Preferred option — use the Python bootstrap script
With your virtual environment active:

```bash
python init_db.py
```

This applies the current canonical schema and template registrations using the database settings from `.env` through `settings.py`.

### Manual option
From the deployable folder:

```bash
psql -U postgres -d synctide -f schema.sql
psql -U postgres -d synctide -f register_templates.sql
```

## 5) Place the Excel templates
Place these files inside:

```text
report_templates/
```

Expected files:
- `Report_template_1.xlsx`
- `Daily_Report_Template.xlsx`
- `Monthly_Report_Template.xlsx`
- `Yearly_Report_Template.xlsx`

## 6) Start the platform
After the DB is ready:

```bash
start_backend.bat
start_ui.bat
```

## 7) Optional verification
You can check the tables with:

```bash
psql -U postgres -d synctide -c "\dt"
```

Expected core tables include:
- devices
- measurements
- tag_metadata
- source_files
- system_tags
- device_tag_mappings
- device_alarm_rules
- reports
- report_templates
- report_jobs
- report_outputs
- users
- user_sessions
