Today I started using the “Azure Database for PostreSQL” PaaS service offering. It went pretty smoothly, but connecting took a little more effort than I expected (all for good reasons!).
Creating the PostreSQL Service
You can find the creation screen in the Azure portal by pressing (+), clicking Databases, and scrolling down.
As with most things in Azure, creating the service through the portal was pretty trivial. You basically just provide the name, region, resource group, subscription, select the size you want, specify a user + password, and you’re done! It takes around a minute to complete with a smallish database size.
Connecting to the Database
We’re going to connect with DBeaver (its like SQuirreL and DBVizualizer if you haven’t heard of it). Then we will also connect with the “psql” command line utility from Linux. This should be pretty quick – but there are two wrenches in the works:
- SSL is enabled.
- Azure has blocked all inbound IPs by default – nothing can connect in.
Connecting with DBeaver
- Go to your Postgres instance in the portal and view the “Overview” screen.
- Open DBeaver, create a new Postgres connection.
- Copy the server name from the portal into the host section of DBeaver.
- Copy the Server Admin Login name from the portal into the user name section of DBeaver.
- Type in your password for that Admin user.
- Set the database as Postgres in DBeaver.
- You can leave the port as the default 5432.
- Now, go to driver properties on the left of DBeaver and set:
- ssl to true
- sslmode to require
This is shown here:
At this point, you’ve got all the connection details in DBeaver set up properly; but you still can’t connect. You’ll have to go into the Azure portal, click “Connection Security”, and then create a firewall rule that allows your IP in. You can also, alternatively, add in a pre-defined subnet you have for yourself, your company, etc. At that point, everything on that subnet will be able to connect properly.
After this, you should be able to “Test Connection” successfully.
Connecting with PSQL from Centos 7
Assuming you opened up the firewall or subnet as noted at the end of the previous example with DBeaver, you can then just:
Install the PSQL client library:
- sudo yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
- sudo yum install postgresql10
And connect with the psql utility:
- psql “sslmode=require host=yourhost.postgres.database.azure.com dbname=postgres user=youruser@yourhost”