Created
September 6, 2025 12:54
-
-
Save biswajit-saha/202efc5dd351e2e2032ecde9dc493d20 to your computer and use it in GitHub Desktop.
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
| function pg { | |
| param( | |
| [Parameter(Position=0, Mandatory=$true)] | |
| [ValidateSet("start","stop","restart","status","config")] | |
| [string]$Action | |
| ) | |
| $configPath = Join-Path $env:APPDATA "pg-config.json" | |
| function Get-ServiceName { | |
| if (Test-Path $configPath) { | |
| $cfg = Get-Content $configPath | ConvertFrom-Json | |
| return $cfg.ServiceName | |
| } else { | |
| return Select-AndSaveService | |
| } | |
| } | |
| function Select-AndSaveService { | |
| $services = Get-Service -Name "postgresql*" -ErrorAction SilentlyContinue | |
| if (-not $services) { | |
| Write-Host "No PostgreSQL services found.`n" | |
| return $null | |
| } elseif ($services.Count -eq 1) { | |
| $selected = $services[0].Name | |
| } else { | |
| Write-Host "Multiple PostgreSQL services found:`n" | |
| for ($i=0; $i -lt $services.Count; $i++) { | |
| Write-Host "$($i+1). $($services[$i].Name) ($($services[$i].DisplayName))" | |
| } | |
| $choice = Read-Host "Select service number" | |
| $selected = $services[[int]$choice - 1].Name | |
| } | |
| @{ ServiceName = $selected } | ConvertTo-Json | Set-Content $configPath | |
| Write-Host "Saved PostgreSQL service: $selected`n" | |
| return $selected | |
| } | |
| if ($Action -eq "config") { | |
| if (Test-Path $configPath) { | |
| $cfg = Get-Content $configPath | ConvertFrom-Json | |
| Write-Host "Current PostgreSQL service: $($cfg.ServiceName)`n" | |
| $reconfig = Read-Host "Do you want to reconfigure the service? (y/n)" | |
| if ($reconfig -match '^[Yy]') { | |
| Select-AndSaveService | |
| } else { | |
| Write-Host "Keeping existing configuration.`n" | |
| } | |
| } else { | |
| Write-Host "No PostgreSQL service configured yet.`n" | |
| Select-AndSaveService | |
| } | |
| return | |
| } | |
| $serviceName = Get-ServiceName | |
| if (-not $serviceName) { return } | |
| function Get-ServiceStatus { | |
| try { | |
| (Get-Service -Name $serviceName).Status | |
| } catch { | |
| Write-Host "Cannot get service status. Make sure sudo is installed and you have permission.`n" | |
| return $null | |
| } | |
| } | |
| function Run-SudoCommand { | |
| param([string]$cmd) | |
| try { | |
| sudo pwsh -NoProfile -Command $cmd | |
| } catch { | |
| Write-Host "Failed to execute: $cmd`n" | |
| } | |
| } | |
| switch ($Action) { | |
| "start" { | |
| $status = Get-ServiceStatus | |
| if ($status -eq "Running") { | |
| Write-Host -ForegroundColor Green "PostgreSQL ($serviceName) is already running.`n" | |
| } else { | |
| Write-Host -ForegroundColor Yellow "Starting PostgreSQL ($serviceName)…`n" | |
| Run-SudoCommand "Start-Service -Name '$serviceName'" | |
| $status = Get-ServiceStatus | |
| if ($status -eq "Running") { Write-Host -ForegroundColor Green "PostgreSQL ($serviceName) started.`n" } | |
| else { Write-Host -ForegroundColor Red "Failed to start PostgreSQL ($serviceName). Status: $status`n" } | |
| } | |
| } | |
| "stop" { | |
| $status = Get-ServiceStatus | |
| if ($status -eq "Stopped") { | |
| Write-Host -ForegroundColor Green "PostgreSQL ($serviceName) is already stopped.`n" | |
| } else { | |
| Write-Host -ForegroundColor Yellow "Stopping PostgreSQL ($serviceName)…`n" | |
| Run-SudoCommand "Stop-Service -Name '$serviceName'" | |
| $status = Get-ServiceStatus | |
| if ($status -eq "Stopped") { Write-Host -ForegroundColor Green "PostgreSQL ($serviceName) stopped.`n" } | |
| else { Write-Host -ForegroundColor Red "Failed to stop PostgreSQL ($serviceName). Status: $status`n" } | |
| } | |
| } | |
| "restart" { | |
| Write-Host -ForegroundColor Yellow "Restarting PostgreSQL ($serviceName)…`n" | |
| Run-SudoCommand "Restart-Service -Name '$serviceName' -Force" | |
| $status = Get-ServiceStatus | |
| if ($status -eq "Running") { Write-Host -ForegroundColor Green "PostgreSQL ($serviceName) restarted.`n" } | |
| else { Write-Host -ForegroundColor Red "Failed to restart PostgreSQL ($serviceName). Status: $status`n" } | |
| } | |
| "status" { | |
| $status = Get-ServiceStatus | |
| Write-Host -ForegroundColor Cyan "PostgreSQL ($serviceName) current status: $status`n" | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pg.ps1 – Simple PostgreSQL Service Manager for Windows
This PowerShell function
pgallows you to easily start, stop, restart, or check the status of your PostgreSQL service on Windows. It supports auto-detecting the PostgreSQL service and saving your configuration for future use. The function uses sudo for Windows to ensure services can be started/stopped with proper permissions.Built using help of ChatGPT and tested manually.