-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-database.ps1
More file actions
102 lines (89 loc) · 3.55 KB
/
start-database.ps1
File metadata and controls
102 lines (89 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Use this script to start a Docker container for a local development database on Windows.
# Requirements:
# - Docker Desktop must be installed and running: https://www.docker.com/products/docker-desktop/
# - Run this script from the root of the project in PowerShell
# Load environment variables from .env
$envFile = Join-Path $PSScriptRoot ".env"
if (-Not (Test-Path $envFile)) {
Write-Error ".env file not found. Please copy .env.example to .env and configure it."
exit 1
}
Get-Content $envFile | ForEach-Object {
if ($_ -match '^\s*([^#][^=]+)=(.*)$') {
[System.Environment]::SetEnvironmentVariable($matches[1].Trim(), $matches[2].Trim())
}
}
$DATABASE_URL = [System.Environment]::GetEnvironmentVariable("DATABASE_URL")
if (-Not $DATABASE_URL) {
Write-Error "DATABASE_URL is not set in .env"
exit 1
}
# Parse DATABASE_URL: postgresql://user:password@host:port/dbname
if ($DATABASE_URL -match 'postgresql://([^:]+):([^@]+)@([^:]+):(\d+)/(.+)') {
$DB_PASSWORD = $matches[2]
$DB_PORT = $matches[4]
$DB_NAME = $matches[5]
} else {
Write-Error "Could not parse DATABASE_URL. Expected format: postgresql://user:password@host:port/dbname"
exit 1
}
$DB_CONTAINER_NAME = "$DB_NAME-postgres"
# Check Docker is installed
if (-Not (Get-Command docker -ErrorAction SilentlyContinue)) {
Write-Error "Docker is not installed or not in PATH.`nInstall Docker Desktop: https://www.docker.com/products/docker-desktop/"
exit 1
}
# Check Docker daemon is running
$dockerInfo = docker info 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker Desktop is not running. Please open Docker Desktop and try again.`nhttps://www.docker.com/products/docker-desktop/"
exit 1
}
# Check if the port is already in use
$portInUse = Get-NetTCPConnection -LocalPort $DB_PORT -ErrorAction SilentlyContinue
if ($portInUse) {
Write-Error "Port $DB_PORT is already in use. Please free the port and try again."
exit 1
}
# Check if the container is already running
$running = docker ps -q -f "name=$DB_CONTAINER_NAME" 2>&1
if ($running) {
Write-Host "Database container '$DB_CONTAINER_NAME' is already running."
exit 0
}
# Check if the container exists but is stopped
$existing = docker ps -q -a -f "name=$DB_CONTAINER_NAME" 2>&1
if ($existing) {
docker start $DB_CONTAINER_NAME
Write-Host "Existing database container '$DB_CONTAINER_NAME' started."
exit 0
}
# Warn if default password is used
if ($DB_PASSWORD -eq "password") {
Write-Warning "You are using the default database password."
$reply = Read-Host "Should we generate a random password for you? [y/N]"
if ($reply -notmatch '^[Yy]$') {
Write-Host "Please change the default password in the .env file and try again."
exit 1
}
# Generate a random URL-safe password
$bytes = New-Object Byte[] 12
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($bytes)
$DB_PASSWORD = [Convert]::ToBase64String($bytes) -replace '\+', '-' -replace '/', '_' -replace '='
(Get-Content $envFile) -replace ':password@', ":$DB_PASSWORD@" | Set-Content $envFile
Write-Host "New password generated and saved to .env"
}
# Create and start the container
docker run -d `
--name $DB_CONTAINER_NAME `
-e POSTGRES_USER="postgres" `
-e POSTGRES_PASSWORD="$DB_PASSWORD" `
-e POSTGRES_DB="$DB_NAME" `
-p "${DB_PORT}:5432" `
docker.io/postgres
if ($LASTEXITCODE -eq 0) {
Write-Host "Database container '$DB_CONTAINER_NAME' was successfully created."
} else {
Write-Error "Failed to create the database container."
exit 1
}