= Connect Azure Database for PostgreSQL Flexible Server with the private access connectivity method =
**APPLIES TO
Azure Database for PostgreSQL - Flexible Server
Azure Database for PostgreSQL Flexible Server is a managed service that you can use to run, manage, and scale highly available PostgreSQL servers in the cloud. This quickstart shows you how to create a flexible server in a virtual network by using the Azure portal

If you don't have an Azure subscription, create a free Azure account before you begin

== Sign in to the Azure portal ==
Go to the Azure portal. Enter your credentials to sign in to the portal. The default view is your service dashboard

== Create an Azure Database for PostgreSQL flexible server ==
You create a flexible server with a defined set of compute and storage resources. You create the server within an Azure resource group

Complete these steps to create a flexible server:
Search for and select
Azure Database for PostgreSQL serversin the portal:
Select
Add

On the
Select Azure Database for PostgreSQL deployment optionpage, select Flexible serveras the deployment option:
On the
Basicstab, enter the subscription, resource group, region, and server name. With the default values, this will provision a PostgreSQL server of version 12 with General purpose pricing tier using 2 vCores, 8 GiB RAM, and 28 GiB storage. The backup retention is sevendays. You can use Developmentworkload to default to a lower-cost pricing tier

In the
Basicstab, enter a unique admin usernameand admin password

Go to the
Networkingtab, and select private access. You can't change the connectivity method after you create the server. Select Create virtual networkto create a new virtual network vnetenvironment1. Select OKonce you have provided the virtual network name and subnet information

Select
Review + createto review your flexible server configuration

Select
Createto provision the server. Provisioning can take a few minutes

Wait until the deployment is complete and successful

Select
Go to resourceto view the server's Overviewpage opens

== Create an Azure Linux virtual machine ==
Since the server is in a virtual network, you can only connect to the server from other Azure services in the same virtual network as the server. To connect and manage the server, let's create a Linux virtual machine. The virtual machine must be created in the
**same region** and **same subscription The Linux virtual machine can be used as an SSH tunnel to manage your database server


Go to your resource group in which the server was created. Select
Add

Select
Ubuntu Server 18.04 LTS

In the
Basicstab, under Project details, make sure the correct subscription is selected and then choose to Create newresource group. Type myResourceGroupfor the name

Under
Instance details, type myVMfor the Virtual machine name, and choose the same Regionas your database server

Under
Administrator account, select SSH public key

In
Usernametype azureuser

For
SSH public key source, leave the default of Generate new key pair, and then type myKeyfor the Key pair name

Under
Inbound port rules> Public inbound ports, choose Allow selected portsand then select SSH (22)and HTTP (80)from the drop-down

Select the
Networkingpage to configure the virtual network. For the virtual network, choose the vnetenvironment1created for the database server

Select
Manage subnet configurationto create a new subnet for the server

Add the new subnet for the virtual machine

After the subnet has been created successfully, close the page

Select
Review + Create

Select
Create. When the Generate new key pairwindow opens, select Download private key and create resource. Your key file will be downloaded as myKey.pem

Important
Make sure you know where the
.pemfile was downloaded. You will need the path to it in the next step

When the deployment is finished, select
Go to resourceto view the virtual machine Overviewpage

Select the public IP address and copy it to your clipboard

== Install PostgreSQL client tools ==
Create an SSH connection with the VM using Bash or PowerShell. At your prompt, open an SSH connection to your virtual machine. Replace the IP address with the one from your VM, and replace the path to the
.pem with the path to where the key file was downloaded

ssh -i .\Downloads\myKey1.pem [email protected]
Tip
The SSH key you created can be used the next time you create a VM in Azure. Just select the
**Use a key stored in Azure** for **SSH public key source** the next time you create a VM. You already have the private key on your computer, so you won't need to download anything

You need to install the postgresql-client tool to be able to connect to the server

sudo apt-get update sudo apt-get install postgresql-client
Connections to the database are enforced with SSL, hence you need to download the public SSL certificate

wget --no-check-certificate httpsdl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem
== Connect to the server from Azure Linux virtual machine ==
With the
**psql** client tool installed, we can now connect to the server from your local environment


psql --host=mydemoserver-pg.postgres.database.azure.com --port=5432 --username=myadmin --dbname=postgres --set=sslmode=require --set=sslrootcert=DigiCertGlobalRootCA.crt.pem
== Clean up resources ==
You have now created an Azure Database for PostgreSQL flexible server in a resource group. If you don't expect to need these resources in the future, you can delete them by deleting the resource group, or you can just delete the PostgreSQL server. To delete the resource group, complete the following steps:
- In the Azure portal, search for and select
Resource groups

- In the list of resource groups, select the name of your resource group

- In the
Overviewpage for your resource group, select Delete resource group

- In the confirmation dialog box, type the name of your resource group, and then select
Delete

== Next steps ==
== Feedback ==
Submit and view feedback for