Files

390 lines
19 KiB
TypeScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { PostgreSQLManagementFlexibleServerClient } from "@azure/arm-postgresql-flexible";
import { DefaultAzureCredential } from "@azure/identity";
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersClusterCreate.json
*/
async function createANewElasticCluster(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
location: "eastus",
administratorLogin: "examplelogin",
administratorLoginPassword: "examplepassword",
backup: { backupRetentionDays: 7, geoRedundantBackup: "Disabled" },
cluster: { clusterSize: 2, defaultDatabaseName: "clusterdb" },
createMode: "Create",
highAvailability: { mode: "Disabled" },
network: { publicNetworkAccess: "Disabled" },
storage: { autoGrow: "Disabled", storageSizeGB: 256, tier: "P15" },
version: "16",
sku: { name: "Standard_D4ds_v5", tier: "GeneralPurpose" },
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateGeoRestoreWithDataEncryptionEnabled.json
*/
async function createANewServerUsingARestoreOfAGeographicallyRedundantBackupOfAnExistingServerWithDataEncryptionBasedOnCustomerManagedKey(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
identity: {
type: "UserAssigned",
userAssignedIdentities: {
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/examplegeoredundantidentity":
{},
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity":
{},
},
},
location: "eastus",
createMode: "GeoRestore",
dataEncryption: {
type: "AzureKeyVault",
geoBackupKeyURI:
"https://examplegeoredundantkeyvault.vault.azure.net/keys/examplekey/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
geoBackupUserAssignedIdentityId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/examplegeoredundantidentity",
primaryKeyURI:
"https://exampleprimarykeyvault.vault.azure.net/keys/examplekey/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
primaryUserAssignedIdentityId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity",
},
pointInTimeUTC: new Date("2025-06-01T18:35:22.123456Z"),
sourceServerResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/examplesourceserver",
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateGeoRestoreWithDataEncryptionEnabledAutoUpdate.json
*/
async function createANewServerUsingARestoreOfAGeographicallyRedundantBackupOfAnExistingServerWithDataEncryptionBasedOnCustomerManagedKeyWithAutomaticKeyVersionUpdate(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
identity: {
type: "UserAssigned",
userAssignedIdentities: {
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/examplegeoredundantidentity":
{},
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity":
{},
},
},
location: "eastus",
createMode: "GeoRestore",
dataEncryption: {
type: "AzureKeyVault",
geoBackupKeyURI: "https://examplegeoredundantkeyvault.vault.azure.net/keys/examplekey",
geoBackupUserAssignedIdentityId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/examplegeoredundantidentity",
primaryKeyURI: "https://exampleprimarykeyvault.vault.azure.net/keys/examplekey",
primaryUserAssignedIdentityId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity",
},
pointInTimeUTC: new Date("2025-06-01T18:35:22.123456Z"),
sourceServerResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/examplesourceserver",
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateInMicrosoftOwnedVirtualNetworkWithZoneRedundantHighAvailability.json
*/
async function createANewServerInMicrosoftOwnedVirtualNetworkWithZoneRedundantHighAvailability(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
location: "eastus",
administratorLogin: "exampleadministratorlogin",
administratorLoginPassword: "examplepassword",
availabilityZone: "1",
backup: { backupRetentionDays: 7, geoRedundantBackup: "Enabled" },
createMode: "Create",
highAvailability: { mode: "ZoneRedundant" },
network: { publicNetworkAccess: "Enabled" },
storage: { autoGrow: "Disabled", storageSizeGB: 512, tier: "P20" },
version: "17",
sku: { name: "Standard_D4ds_v5", tier: "GeneralPurpose" },
tags: { InCustomerVnet: "false", InMicrosoftVnet: "true" },
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateInYourOwnVirtualNetworkWithSameZoneHighAvailability.json
*/
async function createANewServerInYourOwnVirtualNetworkWithSameZoneHighAvailability(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
location: "eastus",
administratorLogin: "exampleadministratorlogin",
administratorLoginPassword: "examplepassword",
availabilityZone: "1",
backup: { backupRetentionDays: 7, geoRedundantBackup: "Enabled" },
createMode: "Create",
highAvailability: { mode: "SameZone" },
network: {
delegatedSubnetResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.Network/virtualNetworks/examplevirtualnetwork/subnets/examplesubnet",
privateDnsZoneArmResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.Network/privateDnsZones/exampleprivatednszone.private.postgres.database",
},
storage: { autoGrow: "Disabled", storageSizeGB: 512, tier: "P20" },
version: "17",
sku: { name: "Standard_D4ds_v5", tier: "GeneralPurpose" },
tags: { InCustomerVnet: "true", InMicrosoftVnet: "false" },
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreatePointInTimeRestore.json
*/
async function createANewServerUsingAPointInTimeRestoreOfABackupOfAnExistingServer(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
location: "eastus",
createMode: "PointInTimeRestore",
pointInTimeUTC: new Date("2025-06-01T18:35:22.123456Z"),
sourceServerResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/examplesourceserver",
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateReplica.json
*/
async function createAReadReplicaOfAnExistingServer(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
identity: {
type: "UserAssigned",
userAssignedIdentities: {
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity":
{},
},
},
location: "eastus",
createMode: "Replica",
dataEncryption: {
type: "AzureKeyVault",
geoBackupKeyURI: "",
geoBackupUserAssignedIdentityId: "",
primaryKeyURI:
"https://exampleprimarykeyvault.vault.azure.net/keys/examplekey/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
primaryUserAssignedIdentityId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity",
},
sourceServerResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/examplesourceserver",
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateReviveDropped.json
*/
async function createANewServerUsingABackupOfAServerThatWasDeletedOrDroppedRecently(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
location: "eastus",
createMode: "ReviveDropped",
pointInTimeUTC: new Date("2025-06-01T18:30:22.123456Z"),
sourceServerResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/exampledeletedserver",
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateWithDataEncryptionEnabled.json
*/
async function createANewServerWithDataEncryptionBasedOnCustomerManagedKey(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
identity: {
type: "UserAssigned",
userAssignedIdentities: {
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity":
{},
},
},
location: "eastus",
administratorLogin: "exampleadministratorlogin",
administratorLoginPassword: "examplepassword",
availabilityZone: "1",
backup: { backupRetentionDays: 7, geoRedundantBackup: "Disabled" },
createMode: "Create",
dataEncryption: {
type: "AzureKeyVault",
geoBackupKeyURI: "",
geoBackupUserAssignedIdentityId: "",
primaryKeyURI:
"https://exampleprimarykeyvault.vault.azure.net/keys/examplekey/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
primaryUserAssignedIdentityId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity",
},
highAvailability: { mode: "ZoneRedundant" },
network: {
delegatedSubnetResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.Network/virtualNetworks/examplevirtualnetwork/subnets/examplesubnet",
privateDnsZoneArmResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourcegroups/exampleresourcegroup/providers/Microsoft.Network/privateDnsZones/exampleprivatednszone.postgres.database.azure.com",
},
storage: { autoGrow: "Disabled", storageSizeGB: 512, tier: "P20" },
version: "17",
sku: { name: "Standard_D4ds_v5", tier: "GeneralPurpose" },
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateWithDataEncryptionEnabledAutoUpdate.json
*/
async function createANewServerWithDataEncryptionBasedOnCustomerManagedKeyWithAutomaticKeyVersionUpdate(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
identity: {
type: "UserAssigned",
userAssignedIdentities: {
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity":
{},
},
},
location: "eastus",
administratorLogin: "exampleadministratorlogin",
administratorLoginPassword: "examplepassword",
availabilityZone: "1",
backup: { backupRetentionDays: 7, geoRedundantBackup: "Disabled" },
createMode: "Create",
dataEncryption: {
type: "AzureKeyVault",
geoBackupKeyURI: "",
geoBackupUserAssignedIdentityId: "",
primaryKeyURI: "https://exampleprimarykeyvault.vault.azure.net/keys/examplekey",
primaryUserAssignedIdentityId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/exampleprimaryidentity",
},
highAvailability: { mode: "ZoneRedundant" },
network: {
delegatedSubnetResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.Network/virtualNetworks/examplevirtualnetwork/subnets/examplesubnet",
privateDnsZoneArmResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourcegroups/exampleresourcegroup/providers/Microsoft.Network/privateDnsZones/exampleprivatednszone.postgres.database.azure.com",
},
storage: { autoGrow: "Disabled", storageSizeGB: 512, tier: "P20" },
version: "17",
sku: { name: "Standard_D4ds_v5", tier: "GeneralPurpose" },
});
console.log(result);
}
/**
* This sample demonstrates how to creates a new server.
*
* @summary creates a new server.
* x-ms-original-file: 2026-04-01-preview/ServersCreateWithMicrosoftEntraEnabledInYourOwnVirtualNetworkWithoutHighAvailability.json
*/
async function createANewServerWithMicrosoftEntraAuthenticationEnabledInYourOwnVirtualNetworkAndWithoutHighAvailability(): Promise<void> {
const credential = new DefaultAzureCredential();
const subscriptionId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const client = new PostgreSQLManagementFlexibleServerClient(credential, subscriptionId);
const result = await client.servers.createOrUpdate("exampleresourcegroup", "exampleserver", {
location: "eastus",
administratorLogin: "exampleadministratorlogin",
administratorLoginPassword: "examplepassword",
authConfig: {
activeDirectoryAuth: "Enabled",
passwordAuth: "Enabled",
tenantId: "tttttt-tttt-tttt-tttt-tttttttttttt",
},
availabilityZone: "1",
backup: { backupRetentionDays: 7, geoRedundantBackup: "Disabled" },
createMode: "Create",
dataEncryption: { type: "SystemManaged" },
highAvailability: { mode: "Disabled" },
network: {
delegatedSubnetResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/exampleresourcegroup/providers/Microsoft.Network/virtualNetworks/examplevirtualnetwork/subnets/examplesubnet",
privateDnsZoneArmResourceId:
"/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourcegroups/exampleresourcegroup/providers/Microsoft.Network/privateDnsZones/exampleprivatednszone.postgres.database.azure.com",
},
storage: { autoGrow: "Disabled", storageSizeGB: 512, tier: "P20" },
version: "17",
sku: { name: "Standard_D4ds_v5", tier: "GeneralPurpose" },
});
console.log(result);
}
async function main(): Promise<void> {
await createANewElasticCluster();
await createANewServerUsingARestoreOfAGeographicallyRedundantBackupOfAnExistingServerWithDataEncryptionBasedOnCustomerManagedKey();
await createANewServerUsingARestoreOfAGeographicallyRedundantBackupOfAnExistingServerWithDataEncryptionBasedOnCustomerManagedKeyWithAutomaticKeyVersionUpdate();
await createANewServerInMicrosoftOwnedVirtualNetworkWithZoneRedundantHighAvailability();
await createANewServerInYourOwnVirtualNetworkWithSameZoneHighAvailability();
await createANewServerUsingAPointInTimeRestoreOfABackupOfAnExistingServer();
await createAReadReplicaOfAnExistingServer();
await createANewServerUsingABackupOfAServerThatWasDeletedOrDroppedRecently();
await createANewServerWithDataEncryptionBasedOnCustomerManagedKey();
await createANewServerWithDataEncryptionBasedOnCustomerManagedKeyWithAutomaticKeyVersionUpdate();
await createANewServerWithMicrosoftEntraAuthenticationEnabledInYourOwnVirtualNetworkAndWithoutHighAvailability();
}
main().catch(console.error);