-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCreatePostgreSQLFlexibleServer.ps1
More file actions
142 lines (119 loc) · 4.85 KB
/
Copy pathCreatePostgreSQLFlexibleServer.ps1
File metadata and controls
142 lines (119 loc) · 4.85 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#Requires -Modules Az.PostgreSQL
if(-not(Get-AzResourceProvider -ProviderNamespace Microsoft.DBforPostgreSQL))
{
Register-AzResourceProvider -ProviderNamespace Microsoft.DBforPostgreSQL
}
function create-AzPGFlexibleServer
{
<#
Written by: Tim Chapman, Microsoft 09/2021
.SYNOPSIS
Wrapper script to quickly create an Azure Database for PostgreSQL Flexible Server
.DESCRIPTION
Wrapper script to quickly create an Azure Database for PostgreSQL Flexible Server.
Will create a Resource Group if it doesn't already exist.
Will create a firewall rule for your current public IP address.
.EXAMPLE
$PGParams = @{}
$PGParams.RGName = "timchappgtestrg"
$PGParams.Location = "eastus"
$PGParams.PGServerName = "timchapflexpgtest"
$PGParams.PGAdminUserName = "timchapman"
$PGParams.PGAdminPassword = "Password12345!!"
$PGParams.PGSkuTier = "GeneralPurpose"
$PGParams.PGSku= "Standard_D2s_v3"
$PGFlexServer = create-AzPGFlexibleServer @PGParams
.PARAMETER RGName
The name of the resource group. Will be created if it does not exist in the current subscription.
.PARAMETER Location
Region to put the PostgreSQL Flexible Server.
.PARAMETER PGServerName
Name of the PostgreSQL Flexible Server
.PARAMETER PGAdminUserName
Administrator username for the server. Once set, it cannot be changed.
.PARAMETER PGAdminPassword
The password of the administrator. Minimum 8 characters and maximum 128 characters. Password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.
.PARAMETER PGSkuTier
Compute tier of the server. Accepted values: Burstable, GeneralPurpose, Memory Optimized. Default: Burstable.
.PARAMETER PGSku
The name of the sku, typically, tier + family + cores, e.g. Standard_B1ms, Standard_D2ds_v4.
.PARAMETER PGVersion
Version of PostgreSQL to be used
.PARAMETER PGStorageInMb
Max storage allowed for a server.
#>
param(
[Parameter(Mandatory = $true, Position=0)]
[string] $RGName ,
[Parameter(Mandatory = $true, Position=1)]
[string] $Location,
[Parameter(Mandatory = $true, Position=2)]
[string] $PGServerName ,
[Parameter(Mandatory = $true, Position=3)]
[string] $PGAdminUserName,
[Parameter(Mandatory = $true, Position=4)]
[string] $PGAdminPassword,
[Parameter(Position=5)]
[ValidateSet("GeneralPurpose", "MemoryOptimized", "Burstable")]
[string] $PGSkuTier = "GeneralPurpose",
[Parameter(Position=6)]
[string] $PGSku = "Standard_B1ms",
[Parameter(Position=7)]
[ValidateScript({$_ -ge 11 -and $_%1 -eq 0})]
[int] $PGVersion = 12 ,
[Parameter(Position = 8)]
[ValidateSet("32768","65536","131072","262144","524288","1048576","2097152","4194304","8388608","16777216")]
[int] $PGStorageInMb = 32768
)
if(-not(Get-AzContext))
{
Login-AzAccount
}
#servername must be lowercase or the deployment will fail.
$PGServerName = $PGServerName.ToLower()
$SkuMatch = $false
if ($PGSkuTier -eq "Burstable" -and $PGSku -like "Standard_B*")
{
$SkuMatch = $true
}
elseif ($PGSkuTier -eq "GeneralPurpose" -and $PGSku -like "Standard_D*")
{
$SkuMatch = $true
}
elseif ($PGSkuTier -eq "MemoryOptimized" -and $PGSku -like "Standard_E*")
{
$SkuMatch = $true
}
else
{
Write-Error "SKU and SKU Tier mismatch"
return
}
if(-not(Get-AzResourceGroup -Name $RGName -Location $Location -ErrorAction Ignore))
{
$RG = New-AzResourceGroup -Name $RGName -Location $Location
}
[SecureString]$Password = ConvertTo-SecureString $PGAdminPassword -AsPlainText -Force
$PostgreSQLParams = @{}
$PostgreSQLParams.Name = $PGServerName
$PostgreSQLParams.ResourceGroupName = $RGName
$PostgreSQLParams.Location = $Location
$PostgreSQLParams.AdministratorUsername = $PGAdminUserName
$PostgreSQLParams.AdministratorLoginPassword = $Password
$PostgreSQLParams.SkuTier = $PGSkuTier
$PostgreSQLParams.Sku = $PGSku
$PostgreSQLParams.Version = $PGVersion
$PostgreSQLParams.StorageInMb = $PGStorageInMb
$PostgreSQLParams.PublicAccess = "None"
$PGServerObject = New-AzPostgreSqlFlexibleServer @PostgreSQLParams
$MyIPAddress = Invoke-RestMethod http://ipinfo.io/json | select -exp ip
$FirewallRuleName = "pgallowedips-$PGServerName"
$FirewallRules = @{}
$FirewallRules.ResourceGroupName = $RGName
$FirewallRules.ServerName = $PGServerName
$FirewallRules.FirewallRuleName = $FirewallRuleName
$FirewallRules.StartIpAddress = $MyIPAddress
$FirewallRules.EndIpAddress = $MyIPAddress
$ServerFirewallRule = New-AzPostgreSqlFlexibleServerFirewallRule @FirewallRules
return $PGServerObject
}