# Creates local secrets and .env (Windows PowerShell 5.1 / PowerShell 7). # Existing files are NEVER overwritten: the password of an already initialised database volume # must keep matching secrets/postgres_password. $ErrorActionPreference = 'Stop' Set-Location (Join-Path $PSScriptRoot '..') $db = if ($env:POSTGRES_DB) { $env:POSTGRES_DB } else { 'messenger' } $user = if ($env:POSTGRES_USER) { $env:POSTGRES_USER } else { 'messenger' } New-Item -ItemType Directory -Force -Path 'secrets' | Out-Null # UTF-8 WITHOUT BOM and without a trailing newline: readable by the PostgreSQL image and by the app. $utf8 = New-Object System.Text.UTF8Encoding($false) $pwFile = Join-Path (Get-Location) 'secrets/postgres_password' $urlFile = Join-Path (Get-Location) 'secrets/database_url' if (-not (Test-Path $pwFile)) { $bytes = New-Object byte[] 24 [System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes) $password = ($bytes | ForEach-Object { $_.ToString('x2') }) -join '' [System.IO.File]::WriteAllText($pwFile, $password, $utf8) Write-Host 'created secrets/postgres_password' } if (-not (Test-Path $urlFile)) { $password = [System.IO.File]::ReadAllText($pwFile).Trim() [System.IO.File]::WriteAllText($urlFile, "postgres://${user}:${password}@postgres:5432/${db}?sslmode=disable", $utf8) Write-Host 'created secrets/database_url' } if (-not (Test-Path '.env')) { Copy-Item '.env.example' '.env'; Write-Host 'created .env' }