Home
%3CLINGO-SUB%20id%3D%22lingo-sub-690976%22%20slang%3D%22en-US%22%3EUpgrade%20from%20Basic%20to%20General%20Purpose%20or%20Memory%20Optimized%20tiers%20in%20Azure%20Database%20for%20PostgreSQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-690976%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20data-contrast%3D%22auto%22%3EYou%20can%20create%20an%20Azure%20Database%20for%20PostgreSQL%20server%20using%20one%20of%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fconcepts-pricing-tiers%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Ethree%20different%20pricing%20tiers%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%3A%20Basic%2C%20General%20Purpose%2C%20and%20Memory%20Optimized.%20Each%20tier%20is%20differentiated%20by%20the%20amount%20of%20compute%20in%20%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3EvCores%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%20that%20can%20be%20provisioned%2C%20the%20memory%20per%20%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3EvCore%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%2C%20and%20the%20IOPS%20%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3Eguaranteed.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20data-contrast%3D%22auto%22%3EYou%20can%20scale%20a%20server%20between%20the%20General%20Purpose%20and%20Memory%20Optimized%20tiers.%20However%2C%20today%20there%20isn%E2%80%99t%20an%20automated%20way%20to%20change%20a%20server%20from%20the%20Basic%20tier%20to%20the%20General%20Purpose%20or%20Memory%20Optimized%20tiers.%20You%20can%20use%20the%20bash%20script%20below%20to%20simplify%20migrating%20existing%20databases%20from%20a%20Basic%20server%20to%20a%20General%20Purpose%20or%20Memory%20Optimized%20server.%E2%80%AF%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%E2%80%AF%3C%2FSPAN%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B134233117%26quot%3B%3Atrue%2C%26quot%3B134233118%26quot%3B%3Atrue%2C%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335551550%26quot%3B%3A6%2C%26quot%3B335551620%26quot%3B%3A6%2C%26quot%3B335559740%26quot%3B%3A240%7D%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20run%20the%20bash%20script%20from%20a%20VM%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fcloud-shell%2Fquickstart%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eor%20Azure%20Cloud%20Shell%3C%2FA%3E).%20First%2C%20verify%20that%20you%20have%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fcli%2Fazure%2Finstall-azure-cli%3Fview%3Dazure-cli-latest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Einstalled%20latest%20version%20of%20Azure%20CLI%3C%2FA%3E%20and%20the%20pg%20client%20tools%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdownload%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epg_dump%20and%20pg_restore%3C%2FA%3E.%20Because%20PostgreSQL%20utilities%20such%20as%20pg_dump%2Fpg_restore%20are%20not%20necessarily%20forward%20compatible%2C%20be%20sure%20to%20use%20the%20same%20version%20of%20pg_dump%2Fpg_restore%20as%20the%20PostgreSQL%20version%20of%20the%20target%20database.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20the%20latest%20version%20of%20the%20script%2C%20visit%20our%20GitHub%20repo%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fblob%2Fmaster%2Fsamples%2Fscripts%2Fpgmigrate.sh%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fblob%2Fmaster%2Fsamples%2Fscripts%2Fpgmigrate.sh%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%23!%2Fbin%2Fbash%0A%23%0A%23%20PURPOSE%0A%23%20Migrate%20Azure%20Database%20for%20PostgreSQL%20Server%20from%20Basic%20Pricing%20Tier%20to%20General%20Purpose%2FMemory%20Optimized%20Pricing%20Tier%0A%23%20%0A%23%20DESCRIPTION%0A%23%20Pricing%20tiers%20in%20Azure%20Database%20for%20PostgreSQL%20-%20Single%20Server%0A%23%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fpostgresql%2Fconcepts-pricing-tiers%0A%23%20%0A%23%20Currently%20modifying%20pricing%20tier%20(to%20and%20from%20Basic)%20is%20not%20yet%20supported.%20%0A%23%20This%20script%20helps%20in%20migrating%20an%20existing%20database%20from%20Basic%20Pricing%20Tier%20to%20%0A%23%20General%20Purpose%2FMemory%20Optimized%20Pricing%20Tier.%20%0A%23%20This%20script%20uses%20pg_dump%20to%20extract%20a%20PostgreSQL%20database%20into%20a%20dump%20file%0A%23%20and%20pg_restore%20to%20restore%20the%20PostgreSQL%20database%20from%20an%20archive%20file%20created%20by%20pg_dump.%0A%23%0A%23%20Migrate%20your%20PostgreSQL%20database%20using%20dump%20and%20restore%0A%23%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fpostgresql%2Fhowto-migrate-using-dump-and-restore%0A%23%0A%23%20PREREQUISITES%0A%23%20Azure%20CLI%20(https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Finstall-azure-cli%3Fview%3Dazure-cli-latest)%0A%23%20postgresql-client%20tools%20-%20pg_dump%20and%20pg_restore%20(https%3A%2F%2Fwww.postgresql.org%2Fdownload%2Flinux)%0A%23%20%0A%23%20NOTE%0A%23%20Target%20server%20and%20database%20are%20created%20in%20this%20script.%0A%23%20Please%20modify%20the%20script%20to%20specify%20different%20options%20for%20creation%0A%23%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Fpostgres%2Fserver%3Fview%3Dazure-cli-latest%0A%23%20%0A%23%20USAGE%0A%23%20bash%20pgmigrate.sh%0A%23%20%20%20%20%20%20%20%20%20--source-subscription-id%20'ffffffff-ffff-ffff-ffff-ffffffffffff'%0A%23%20%20%20%20%20%20%20%20%20--source-resource-group-name%20'sourceRG'%0A%23%20%20%20%20%20%20%20%20%20--source-server-name%20'sourcepg'%0A%23%20%20%20%20%20%20%20%20%20--source-username%20'sourceuser'%0A%23%20%20%20%20%20%20%20%20%20--source-password%20'P%40ssw0rd1'%0A%23%20%20%20%20%20%20%20%20%20--source-database-name%20'sourcedb'%0A%23%20%20%20%20%20%20%20%20%20--target-subscription-id%20'ffffffff-ffff-ffff-ffff-ffffffffffff'%0A%23%20%20%20%20%20%20%20%20%20--target-resource-group-name%20'targetRG'%0A%23%20%20%20%20%20%20%20%20%20--target-region%20'eastus'%0A%23%20%20%20%20%20%20%20%20%20--target-server-name%20'targetpg'%0A%23%20%20%20%20%20%20%20%20%20--target-username%20'targetuser'%0A%23%20%20%20%20%20%20%20%20%20--target-password%20'P%40ssw0rd2'%0A%23%20%20%20%20%20%20%20%20%20--target-database-name%20'targetdb'%0A%23%20%20%20%20%20%20%20%20%20--target-sku%20'GP_Gen5_2'%0A%23%0A%0Afunction%20usage()%0A%7B%0A%20%20%20%20echo%20%22%22%0A%20%20%20%20echo%20%22PURPOSE%22%0A%20%20%20%20echo%20%22Migrate%20Azure%20Database%20for%20PostgreSQL%20Server%20from%20Basic%20Pricing%20Tier%20to%20General%20Purpose%2FMemory%20Optimized%20Pricing%20Tier%22%0A%20%20%20%20echo%20%22%22%0A%20%20%20%20echo%20%22DESCRIPTION%22%0A%20%20%20%20echo%20%22Pricing%20tiers%20in%20Azure%20Database%20for%20PostgreSQL%20-%20Single%20Server%22%0A%20%20%20%20echo%20%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fpostgresql%2Fconcepts-pricing-tiers%22%0A%20%20%20%20echo%20%22%22%0A%20%20%20%20echo%20%22Currently%20modifying%20pricing%20tier%20(to%20and%20from%20Basic)%20is%20not%20yet%20supported.%22%0A%20%20%20%20echo%20%22This%20script%20helps%20in%20migrating%20an%20existing%20database%20from%20Basic%20Pricing%20Tier%20to%22%0A%20%20%20%20echo%20%22General%20Purpose%2FMemory%20Optimized%20Pricing%20Tier.%22%0A%20%20%20%20echo%20%22This%20script%20uses%20pg_dump%20to%20extract%20a%20PostgreSQL%20database%20into%20a%20dump%20file%22%0A%20%20%20%20echo%20%22and%20pg_restore%20to%20restore%20the%20PostgreSQL%20database%20from%20an%20archive%20file%20created%20by%20pg_dump.%22%0A%20%20%20%20echo%20%22%22%0A%20%20%20%20echo%20%22Migrate%20your%20PostgreSQL%20database%20using%20dump%20and%20restore%22%0A%20%20%20%20echo%20%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fpostgresql%2Fhowto-migrate-using-dump-and-restore%22%0A%20%20%20%20echo%20%22%22%0A%20%20%20%20echo%20%22PREREQUISITES%22%0A%20%20%20%20echo%20%22Azure%20CLI%20(https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Finstall-azure-cli%3Fview%3Dazure-cli-latest)%22%0A%20%20%20%20echo%20%22postgresql-client%20tools%20-%20pg_dump%20and%20pg_restore%20(https%3A%2F%2Fwww.postgresql.org%2Fdownload%2Flinux)%22%0A%20%20%20%20echo%20%22%22%0A%20%20%20%20echo%20%22NOTE%22%0A%20%20%20%20echo%20%22Target%20server%20and%20database%20are%20created%20in%20this%20script.%22%0A%20%20%20%20echo%20%22Please%20modify%20the%20script%20to%20specify%20different%20options%20for%20server%20creation%22%0A%20%20%20%20echo%20%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Fpostgres%2Fserver%3Fview%3Dazure-cli-latest%22%0A%20%20%20%20echo%20%22%22%0A%20%20%20%20echo%20%22USAGE%22%0A%20%20%20%20echo%20%22bash%20pgmigrate.sh%22%0A%20%20%20%20echo%20-e%20%22%5Ct--source-subscription-id%20'ffffffff-ffff-ffff-ffff-ffffffffffff'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--source-resource-group-name%20'sourceRG'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--source-server-name%20'sourcepg'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--source-username%20'sourceuser'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--source-password%20'P%40ssw0rd1'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--source-database-name%20'sourcedb'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-subscription-id%20'ffffffff-ffff-ffff-ffff-ffffffffffff'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-resource-group-name%20'targetRG'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-region%20'eastus'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-server-name%20'targetpg'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-username%20'targetuser'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-password%20'P%40ssw0rd2'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-database-name%20'targetdb'%22%0A%20%20%20%20echo%20-e%20%22%5Ct--target-sku%20'GP_Gen5_2'%22%0A%20%20%20%20echo%20%22%22%0A%7D%0A%0APARAMS%3D%22%22%0Awhile%20((%20%22%24%23%22%20))%3B%20do%0A%20%20case%20%22%241%22%20in%0A%20%20%20%20-h%20%7C%20--help)%0A%20%20%20%20%20%20usage%0A%20%20%20%20%20%20exit%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-a%7C--source-subscription-id)%0A%20%20%20%20%20%20SOURCE_SUBSCRIPTION_ID%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-b%7C--source-resource-group-name)%0A%20%20%20%20%20%20SOURCE_RESOURCE_GROUP_NAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-c%7C--source-server-name)%0A%20%20%20%20%20%20SOURCE_SERVER_NAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-d%7C--source-username)%0A%20%20%20%20%20%20SOURCE_USERNAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-e%7C--source-password)%0A%20%20%20%20%20%20SOURCE_PASSWORD%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-f%7C--source-database-name)%0A%20%20%20%20%20%20SOURCE_DATABASE_NAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-g%7C--target-subscription-id)%0A%20%20%20%20%20%20TARGET_SUBSCRIPTION_ID%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-h%7C--target-resource-group-name)%0A%20%20%20%20%20%20TARGET_RESOURCE_GROUP_NAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-i%7C--target-region)%0A%20%20%20%20%20%20TARGET_REGION%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-j%7C--target-server-name)%0A%20%20%20%20%20%20TARGET_SERVER_NAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-k%7C--target-username)%0A%20%20%20%20%20%20TARGET_USERNAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-l%7C--target-password)%0A%20%20%20%20%20%20TARGET_PASSWORD%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-m%7C--target-database-name)%0A%20%20%20%20%20%20TARGET_DATABASE_NAME%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-n%7C--target-sku)%0A%20%20%20%20%20%20TARGET_SKU%3D%242%0A%20%20%20%20%20%20shift%202%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20--)%20%23%20end%20argument%20parsing%0A%20%20%20%20%20%20shift%0A%20%20%20%20%20%20break%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20-*%7C--*%3D)%20%23%20unsupported%20flags%0A%20%20%20%20%20%20echo%20%22Error%3A%20Unsupported%20flag%20%241%22%20%26gt%3B%26amp%3B2%0A%20%20%20%20%20%20exit%201%0A%20%20%20%20%20%20%3B%3B%0A%20%20%20%20*)%20%23%20preserve%20positional%20arguments%0A%20%20%20%20%20%20PARAMS%3D%22%24PARAMS%20%241%22%0A%20%20%20%20%20%20shift%0A%20%20%20%20%20%20%3B%3B%0A%20%20esac%0Adone%0A%23%20set%20positional%20arguments%20in%20their%20proper%20place%0Aeval%20set%20--%20%22%24PARAMS%22%0A%0A%23%20Prerequisites%20-%20Install%20Azure%20CLI%20and%20postgresql-client%20package%0Aif%20!%20%5B%20-x%20%22%24(command%20-v%20az)%22%20%5D%3B%20then%0A%20%20echo%20'Error%3A%20azure%20cli%20is%20not%20installed.%20Please%20install%20from%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Finstall-azure-cli%3Fview%3Dazure-cli-latest'%20%26gt%3B%26amp%3B2%0A%20%20exit%201%0Afi%0A%0Aif%20!%20%5B%20-x%20%22%24(command%20-v%20pg_dump)%22%20%5D%3B%20then%0A%20%20echo%20'Error%3A%20pg_dump%20command%20does%20not%20exist.%20Please%20install%20from%20https%3A%2F%2Fwww.postgresql.org%2Fdownload'%20%26gt%3B%26amp%3B2%0A%20%20exit%201%0Afi%0A%0Aif%20!%20%5B%20-x%20%22%24(command%20-v%20pg_restore)%22%20%5D%3B%20then%0A%20%20echo%20'Error%3A%20pg_restore%20command%20does%20not%20exist.%20Please%20install%20from%20https%3A%2F%2Fwww.postgresql.org%2Fdownload'%20%26gt%3B%26amp%3B2%0A%20%20exit%201%0Afi%0A%0A%23%20Login%20to%20source%20subscription%0Aaz%20login%0Aaz%20account%20set%20--subscription%20%22%24SOURCE_SUBSCRIPTION_ID%22%0A%0A%23%20Set%20PG%20SSL%20Mode%0Aexport%20PGSSLMODE%3Drequire%0A%0A%23%20Firewall%20rule%20name%20to%20allow%20the%20current%20VM%0Aexport%20FIREWALLRULENAME%3D%22AllowAllIps%24(date%20%2B%25s)%22%0A%0A%23%20Create%20firewall%20rule%20on%20the%20source%20server%20to%20allow%20connections%20from%20current%20VM%0Aaz%20postgres%20server%20firewall-rule%20create%20-g%20%22%24SOURCE_RESOURCE_GROUP_NAME%22%20-s%20%22%24SOURCE_SERVER_NAME%22%20-n%20%22%24FIREWALLRULENAME%22%20--start-ip-address%20%220.0.0.0%22%20--end-ip-address%20%22255.255.255.255%22%0A%0A%23%20Export%20pg%20password%20to%20be%20used%20by%20the%20pg_dump%20command%0Aexport%20PGPASSWORD%3D%24SOURCE_PASSWORD%0A%0A%23%20Export%20PG%20database%20name%20to%20be%20used%20by%20the%20pg_dump%20command%0Aexport%20PGDATABASE%3D%24SOURCE_DATABASE_NAME%0A%0A%23%20Remove%20dump%20file%20if%20it%20already%20exists%0A%5B%20-e%20%22%24SOURCE_DATABASE_NAME.dump%22%20%5D%20%26amp%3B%26amp%3B%20rm%20-f%20%22%24SOURCE_DATABASE_NAME.dump%22%0A%0A%23%20Create%20a%20dump%20file%20of%20the%20source%20db%0Apg_dump%20-Fc%20-v%20--host%3D%22%24SOURCE_SERVER_NAME.postgres.database.azure.com%22%20--username%3D%22%24SOURCE_USERNAME%40%24SOURCE_SERVER_NAME%22%20--file%3D%22%24SOURCE_DATABASE_NAME.dump%22%0A%0A%23%20check%20that%20filesize%20of%20dump%20file%20is%20greater%20than%200%0Aif%20!%20%5B%20-s%20%22%24SOURCE_DATABASE_NAME.dump%22%20%5D%3B%20then%0A%20%20echo%20'Error%20during%20pg_dump'%20%26gt%3B%26amp%3B2%0A%20%20exit%201%0Afi%0A%0A%23%20Login%20to%20target%20subscription%20%0Aaz%20account%20set%20--subscription%20%22%24TARGET_SUBSCRIPTION_ID%22%0A%0A%23%20Set%20target%20server%20password%0Aexport%20PGPASSWORD%3D%24TARGET_PASSWORD%0A%0A%23%20Create%20the%20target%20server%20in%20the%20specified%20region%0Aaz%20postgres%20server%20create%20-l%20%22%24TARGET_REGION%22%20-g%20%22%24TARGET_RESOURCE_GROUP_NAME%22%20-n%20%22%24TARGET_SERVER_NAME%22%20-u%20%22%24TARGET_USERNAME%22%20-p%20%22%24PGPASSWORD%22%20--sku-name%20%22%24TARGET_SKU%22%0A%0A%23%20Create%20the%20target%20database%0Aaz%20postgres%20db%20create%20-g%20%22%24TARGET_RESOURCE_GROUP_NAME%22%20-s%20%22%24TARGET_SERVER_NAME%22%20-n%20%22%24TARGET_DATABASE_NAME%22%0A%0A%23%20Create%20firewall%20rule%20on%20the%20target%20server%20to%20allow%20connections%20from%20current%20VM%0Aaz%20postgres%20server%20firewall-rule%20create%20-g%20%22%24TARGET_RESOURCE_GROUP_NAME%22%20-s%20%22%24TARGET_SERVER_NAME%22%20-n%20%22%24FIREWALLRULENAME%22%20--start-ip-address%20%220.0.0.0%22%20--end-ip-address%20%22255.255.255.255%22%0A%0A%23%20Export%20PG%20database%20name%20to%20be%20used%20by%20the%20pg_restore%20command%0Aexport%20PGDATABASE%3D%24TARGET_DATABASE_NAME%0A%0A%23%20Restore%20the%20database%20using%20source%20database%20dump%20file%0Apg_restore%20-v%20--no-owner%20--host%3D%22%24TARGET_SERVER_NAME.postgres.database.azure.com%22%20--port%3D5432%20--username%3D%22%24TARGET_USERNAME%40%24TARGET_SERVER_NAME%22%20%22%24SOURCE_DATABASE_NAME.dump%22%0A%0A%23%20Delete%20firewall%20rules%20and%20local%20dump%20file%0Aaz%20postgres%20server%20firewall-rule%20delete%20-g%20%22%24SOURCE_RESOURCE_GROUP_NAME%22%20-s%20%22%24SOURCE_SERVER_NAME%22%20-n%20%22%24FIREWALLRULENAME%22%20--yes%0Aaz%20postgres%20server%20firewall-rule%20delete%20-g%20%22%24TARGET_RESOURCE_GROUP_NAME%22%20-s%20%22%24TARGET_SERVER_NAME%22%20-n%20%22%24FIREWALLRULENAME%22%20--yes%0Arm%20-f%20%22%24SOURCE_DATABASE_NAME.dump%22%0A%0A%23%20Logout%0Aazure%20logout%20-u%20%22%24TARGET_SUBSCRIPTION_ID%22%0A%0A%23%20Visit%20Target%20Server%0Aecho%20%22Migration%20Successful%20!!%22%0Aecho%20%22Target%20Server%20%3A%20https%3A%2F%2Fportal.azure.com%2F%23resource%2Fsubscriptions%2F%24TARGET_SUBSCRIPTION_ID%2FresourceGroups%2F%24TARGET_RESOURCE_GROUP_NAME%2Fproviders%2FMicrosoft.DBforPostgreSQL%2Fservers%2F%24TARGET_SERVER_NAME%2Foverview%22%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20have%20trouble%20running%20the%20script%2C%20please%20let%20us%20know%20by%20opening%20an%20issue%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fissues%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fissues%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20feel%20free%20to%20contribute%20any%20updates%20or%20bug%20fixes%20by%20creating%20a%20pull%20request%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fpulls%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fpulls%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-690976%22%20slang%3D%22en-US%22%3E%3CP%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EUpgrade%20from%20Basic%20to%20General%20Purpose%20or%20Memory%20Optimized%20tiers%20in%20Azure%20Database%20for%20PostgreSQL%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20600px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F118085i29BA4C3A757E0103%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22pg.png%22%20title%3D%22pg.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

You can create an Azure Database for PostgreSQL server using one of three different pricing tiers: Basic, General Purpose, and Memory Optimized. Each tier is differentiated by the amount of compute in vCores that can be provisioned, the memory per vCore, and the IOPS guaranteed.

 

You can scale a server between the General Purpose and Memory Optimized tiers. However, today there isn’t an automated way to change a server from the Basic tier to the General Purpose or Memory Optimized tiers. You can use the bash script below to simplify migrating existing databases from a Basic server to a General Purpose or Memory Optimized server.  

 

You can run the bash script from a VM (or Azure Cloud Shell). First, verify that you have installed latest version of Azure CLI and the pg client tools pg_dump and pg_restore. Because PostgreSQL utilities such as pg_dump/pg_restore are not necessarily forward compatible, be sure to use the same version of pg_dump/pg_restore as the PostgreSQL version of the target database.

 

For the latest version of the script, visit our GitHub repo: https://github.com/Azure/azure-postgresql/blob/master/samples/scripts/pgmigrate.sh

 

#!/bin/bash
#
# PURPOSE
# Migrate Azure Database for PostgreSQL Server from Basic Pricing Tier to General Purpose/Memory Optimized Pricing Tier
# 
# DESCRIPTION
# Pricing tiers in Azure Database for PostgreSQL - Single Server
# https://docs.microsoft.com/en-us/azure/postgresql/concepts-pricing-tiers
# 
# Currently modifying pricing tier (to and from Basic) is not yet supported. 
# This script helps in migrating an existing database from Basic Pricing Tier to 
# General Purpose/Memory Optimized Pricing Tier. 
# This script uses pg_dump to extract a PostgreSQL database into a dump file
# and pg_restore to restore the PostgreSQL database from an archive file created by pg_dump.
#
# Migrate your PostgreSQL database using dump and restore
# https://docs.microsoft.com/en-us/azure/postgresql/howto-migrate-using-dump-and-restore
#
# PREREQUISITES
# Azure CLI (https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest)
# postgresql-client tools - pg_dump and pg_restore (https://www.postgresql.org/download/linux)
# 
# NOTE
# Target server and database are created in this script.
# Please modify the script to specify different options for creation
# https://docs.microsoft.com/en-us/cli/azure/postgres/server?view=azure-cli-latest
# 
# USAGE
# bash pgmigrate.sh
#         --source-subscription-id 'ffffffff-ffff-ffff-ffff-ffffffffffff'
#         --source-resource-group-name 'sourceRG'
#         --source-server-name 'sourcepg'
#         --source-username 'sourceuser'
#         --source-password 'P@ssw0rd1'
#         --source-database-name 'sourcedb'
#         --target-subscription-id 'ffffffff-ffff-ffff-ffff-ffffffffffff'
#         --target-resource-group-name 'targetRG'
#         --target-region 'eastus'
#         --target-server-name 'targetpg'
#         --target-username 'targetuser'
#         --target-password 'P@ssw0rd2'
#         --target-database-name 'targetdb'
#         --target-sku 'GP_Gen5_2'
#

function usage()
{
    echo ""
    echo "PURPOSE"
    echo "Migrate Azure Database for PostgreSQL Server from Basic Pricing Tier to General Purpose/Memory Optimized Pricing Tier"
    echo ""
    echo "DESCRIPTION"
    echo "Pricing tiers in Azure Database for PostgreSQL - Single Server"
    echo "https://docs.microsoft.com/en-us/azure/postgresql/concepts-pricing-tiers"
    echo ""
    echo "Currently modifying pricing tier (to and from Basic) is not yet supported."
    echo "This script helps in migrating an existing database from Basic Pricing Tier to"
    echo "General Purpose/Memory Optimized Pricing Tier."
    echo "This script uses pg_dump to extract a PostgreSQL database into a dump file"
    echo "and pg_restore to restore the PostgreSQL database from an archive file created by pg_dump."
    echo ""
    echo "Migrate your PostgreSQL database using dump and restore"
    echo "https://docs.microsoft.com/en-us/azure/postgresql/howto-migrate-using-dump-and-restore"
    echo ""
    echo "PREREQUISITES"
    echo "Azure CLI (https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest)"
    echo "postgresql-client tools - pg_dump and pg_restore (https://www.postgresql.org/download/linux)"
    echo ""
    echo "NOTE"
    echo "Target server and database are created in this script."
    echo "Please modify the script to specify different options for server creation"
    echo "https://docs.microsoft.com/en-us/cli/azure/postgres/server?view=azure-cli-latest"
    echo ""
    echo "USAGE"
    echo "bash pgmigrate.sh"
    echo -e "\t--source-subscription-id 'ffffffff-ffff-ffff-ffff-ffffffffffff'"
    echo -e "\t--source-resource-group-name 'sourceRG'"
    echo -e "\t--source-server-name 'sourcepg'"
    echo -e "\t--source-username 'sourceuser'"
    echo -e "\t--source-password 'P@ssw0rd1'"
    echo -e "\t--source-database-name 'sourcedb'"
    echo -e "\t--target-subscription-id 'ffffffff-ffff-ffff-ffff-ffffffffffff'"
    echo -e "\t--target-resource-group-name 'targetRG'"
    echo -e "\t--target-region 'eastus'"
    echo -e "\t--target-server-name 'targetpg'"
    echo -e "\t--target-username 'targetuser'"
    echo -e "\t--target-password 'P@ssw0rd2'"
    echo -e "\t--target-database-name 'targetdb'"
    echo -e "\t--target-sku 'GP_Gen5_2'"
    echo ""
}

PARAMS=""
while (( "$#" )); do
  case "$1" in
    -h | --help)
      usage
      exit
      ;;
    -a|--source-subscription-id)
      SOURCE_SUBSCRIPTION_ID=$2
      shift 2
      ;;
    -b|--source-resource-group-name)
      SOURCE_RESOURCE_GROUP_NAME=$2
      shift 2
      ;;
    -c|--source-server-name)
      SOURCE_SERVER_NAME=$2
      shift 2
      ;;
    -d|--source-username)
      SOURCE_USERNAME=$2
      shift 2
      ;;
    -e|--source-password)
      SOURCE_PASSWORD=$2
      shift 2
      ;;
    -f|--source-database-name)
      SOURCE_DATABASE_NAME=$2
      shift 2
      ;;
    -g|--target-subscription-id)
      TARGET_SUBSCRIPTION_ID=$2
      shift 2
      ;;
    -h|--target-resource-group-name)
      TARGET_RESOURCE_GROUP_NAME=$2
      shift 2
      ;;
    -i|--target-region)
      TARGET_REGION=$2
      shift 2
      ;;
    -j|--target-server-name)
      TARGET_SERVER_NAME=$2
      shift 2
      ;;
    -k|--target-username)
      TARGET_USERNAME=$2
      shift 2
      ;;
    -l|--target-password)
      TARGET_PASSWORD=$2
      shift 2
      ;;
    -m|--target-database-name)
      TARGET_DATABASE_NAME=$2
      shift 2
      ;;
    -n|--target-sku)
      TARGET_SKU=$2
      shift 2
      ;;
    --) # end argument parsing
      shift
      break
      ;;
    -*|--*=) # unsupported flags
      echo "Error: Unsupported flag $1" >&2
      exit 1
      ;;
    *) # preserve positional arguments
      PARAMS="$PARAMS $1"
      shift
      ;;
  esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"

# Prerequisites - Install Azure CLI and postgresql-client package
if ! [ -x "$(command -v az)" ]; then
  echo 'Error: azure cli is not installed. Please install from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest' >&2
  exit 1
fi

if ! [ -x "$(command -v pg_dump)" ]; then
  echo 'Error: pg_dump command does not exist. Please install from https://www.postgresql.org/download' >&2
  exit 1
fi

if ! [ -x "$(command -v pg_restore)" ]; then
  echo 'Error: pg_restore command does not exist. Please install from https://www.postgresql.org/download' >&2
  exit 1
fi

# Login to source subscription
az login
az account set --subscription "$SOURCE_SUBSCRIPTION_ID"

# Set PG SSL Mode
export PGSSLMODE=require

# Firewall rule name to allow the current VM
export FIREWALLRULENAME="AllowAllIps$(date +%s)"

# Create firewall rule on the source server to allow connections from current VM
az postgres server firewall-rule create -g "$SOURCE_RESOURCE_GROUP_NAME" -s "$SOURCE_SERVER_NAME" -n "$FIREWALLRULENAME" --start-ip-address "0.0.0.0" --end-ip-address "255.255.255.255"

# Export pg password to be used by the pg_dump command
export PGPASSWORD=$SOURCE_PASSWORD

# Export PG database name to be used by the pg_dump command
export PGDATABASE=$SOURCE_DATABASE_NAME

# Remove dump file if it already exists
[ -e "$SOURCE_DATABASE_NAME.dump" ] && rm -f "$SOURCE_DATABASE_NAME.dump"

# Create a dump file of the source db
pg_dump -Fc -v --host="$SOURCE_SERVER_NAME.postgres.database.azure.com" --username="$SOURCE_USERNAME@$SOURCE_SERVER_NAME" --file="$SOURCE_DATABASE_NAME.dump"

# check that filesize of dump file is greater than 0
if ! [ -s "$SOURCE_DATABASE_NAME.dump" ]; then
  echo 'Error during pg_dump' >&2
  exit 1
fi

# Login to target subscription 
az account set --subscription "$TARGET_SUBSCRIPTION_ID"

# Set target server password
export PGPASSWORD=$TARGET_PASSWORD

# Create the target server in the specified region
az postgres server create -l "$TARGET_REGION" -g "$TARGET_RESOURCE_GROUP_NAME" -n "$TARGET_SERVER_NAME" -u "$TARGET_USERNAME" -p "$PGPASSWORD" --sku-name "$TARGET_SKU"

# Create the target database
az postgres db create -g "$TARGET_RESOURCE_GROUP_NAME" -s "$TARGET_SERVER_NAME" -n "$TARGET_DATABASE_NAME"

# Create firewall rule on the target server to allow connections from current VM
az postgres server firewall-rule create -g "$TARGET_RESOURCE_GROUP_NAME" -s "$TARGET_SERVER_NAME" -n "$FIREWALLRULENAME" --start-ip-address "0.0.0.0" --end-ip-address "255.255.255.255"

# Export PG database name to be used by the pg_restore command
export PGDATABASE=$TARGET_DATABASE_NAME

# Restore the database using source database dump file
pg_restore -v --no-owner --host="$TARGET_SERVER_NAME.postgres.database.azure.com" --port=5432 --username="$TARGET_USERNAME@$TARGET_SERVER_NAME" "$SOURCE_DATABASE_NAME.dump"

# Delete firewall rules and local dump file
az postgres server firewall-rule delete -g "$SOURCE_RESOURCE_GROUP_NAME" -s "$SOURCE_SERVER_NAME" -n "$FIREWALLRULENAME" --yes
az postgres server firewall-rule delete -g "$TARGET_RESOURCE_GROUP_NAME" -s "$TARGET_SERVER_NAME" -n "$FIREWALLRULENAME" --yes
rm -f "$SOURCE_DATABASE_NAME.dump"

# Logout
azure logout -u "$TARGET_SUBSCRIPTION_ID"

# Visit Target Server
echo "Migration Successful !!"
echo "Target Server : https://portal.azure.com/#resource/subscriptions/$TARGET_SUBSCRIPTION_ID/resourceGroups/$TARGET_RESOURCE_GROUP_NAME/providers/Microsoft.DBforPostgreSQL/servers/$TARGET_SERVER_NAME/overview"

 

If you have trouble running the script, please let us know by opening an issue: https://github.com/Azure/azure-postgresql/issues

 

Also, feel free to contribute any updates or bug fixes by creating a pull request: https://github.com/Azure/azure-postgresql/pulls

 

Thank you!