Created
July 16, 2024 09:51
-
-
Save hsupu/995239af026998f9151e6176afa81008 to your computer and use it in GitHub Desktop.
Postgres Initializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.LINK | |
https://www.postgresql.org/docs/current/app-pg-ctl.html | |
#> | |
param( | |
[string]$DataDir, | |
[switch]$InitDataDir, | |
[string]$ServiceName, | |
[switch]$Register, | |
[switch]$Unregister, | |
[switch]$Start, | |
[switch]$Stop, | |
[switch]$AdhocInstance, | |
[switch]$CreateDb, | |
[string]$DbName | |
) | |
trap { throw $_ } | |
$ctl = (Get-Command -ErrorAction Stop "pg_ctl.exe").Path | |
$env:LC_ALL = "en_US.UTF-8" | |
& $ctl --version | |
if ($DataDir -eq '') { | |
$DataDir = Join-Path $PSScriptRoot "data" | |
} | |
$Username = "postgres" | |
if ($ServiceName -eq '') { | |
$ServiceName = "postgres" | |
} | |
if ($DbName -eq '') { | |
$DbName = "main" | |
} | |
$script:currentUser = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() | |
function Test-IsAdmin { | |
param( | |
[switch]$NoThrow | |
) | |
$isAdmin = $script:currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
if (-not $isAdmin -and -not $NoThrow) { | |
Write-Error -ErrorAction Stop "Not run as Admin" | |
throw | |
} | |
} | |
if ($Stop) { | |
if ($AdhocInstance) { | |
Write-Host "pg_ctl stop -D $DataDir" | |
& $ctl stop -D $DataDir | |
if ($LASTEXITCODE -ne 0) { | |
Write-Host "pg_ctl stop exited with $LASTEXITCODE" | |
exit 1 | |
} | |
} | |
else { | |
Test-IsAdmin | |
Stop-Service -ErrorAction Stop -Name $ServiceName | |
Get-Service -ErrorAction Stop -Name $ServiceName | |
} | |
} | |
if ($Unregister) { | |
Test-IsAdmin | |
Write-Host "pg_ctl unregister -N $ServiceName" | |
& $ctl unregister -N $ServiceName | |
if ($LASTEXITCODE -ne 0) { | |
Write-Host "pg_ctl unregister exited with $LASTEXITCODE" | |
exit 1 | |
} | |
} | |
if ($InitDataDir) { | |
Write-Host "pg_ctl initdb -D $DataDir" | |
& $ctl initdb -D $DataDir -o "--username=$Username --locale=en_US.UTF-8 --encoding=UTF8 --set config_file=.\postgresql.conf" | |
if ($LASTEXITCODE -ne 0) { | |
Write-Host "pg_ctl initdb exited with $LASTEXITCODE" | |
exit 1 | |
} | |
} | |
if ($Register) { | |
Test-IsAdmin | |
Write-Host "pg_ctl register -N $ServiceName" | |
# -S - start type | |
# -w - wait for server to start | |
& $ctl register -N $ServiceName -D $DataDir -l postgres.log -U LocalSystem -S auto --wait | |
if ($LASTEXITCODE -ne 0) { | |
Write-Host "pg_ctl register exited with $LASTEXITCODE" | |
exit 1 | |
} | |
} | |
if ($Start) { | |
if ($AdhocInstance) { | |
Write-Host "pg_ctl start -D $DataDir" | |
& $ctl start -D $DataDir | |
if ($LASTEXITCODE -ne 0) { | |
Write-Host "pg_ctl start exited with $LASTEXITCODE" | |
exit 1 | |
} | |
} | |
else { | |
Test-IsAdmin | |
Start-Service -ErrorAction Stop -Name $ServiceName | |
Get-Service -ErrorAction Stop -Name $ServiceName | |
} | |
} | |
if ($CreateDb) { | |
& psql -U $Username -c "CREATE DATABASE $DbName WITH OWNER $Username ENCODING 'utf-8';" | |
if ($LASTEXITCODE -ne 0) { | |
Write-Host "psql exited with $LASTEXITCODE" | |
exit 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment