# PostgreSQL setup for SyncTide

The normal SyncTide installer (`SyncTideSetup-1.4.0.exe`) bundles
PostgreSQL 17 + TimescaleDB and configures it automatically, so most
installs need none of the steps below. This guide is only for a **new PC**
where you are pointing SyncTide at a separately-managed PostgreSQL
instance.

## 1) Install PostgreSQL
1. Download and install PostgreSQL 17 (with TimescaleDB) 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
No manual schema step is required. The `synctide` database starts empty;
when `SyncTideBackend` first starts it runs the bundled, idempotent
`NNN_*.sql` migrations automatically and creates every table. The default
report templates are installed and registered automatically as well.

## 5) Start the platform
After the DB is ready, start the Windows services (they auto-start after a
normal install):

```powershell
Start-Service SyncTideBackend
```

The other services (`SyncTideIngestion`, the protocol pollers, etc.) start
alongside it. The React UI is then served by the backend — open
`http://localhost/` (Caddy on port 80) or `http://localhost:8000/ui` in a
browser. There is no separate UI process to start.

## 6) 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
