We discussed about how to setup a PostgreSQL on Azure cloud DaaS (Database as a Service), as a novice user. When it comes to real world use of Postgres, the first thing that comes to mind for a DBA is how to access PostgreSQL logs.

So it is time for exploring Azure command line (CLI) features for PostgreSQL.

Installing Azure Cli

Azure ships platform specific packages like .rpm’s and .deb packages. They are quite easy to install and well documented. Interestingly, there is even a platform independent way of installation. This doesn’t even need root privileges on Linux and can setup cli for a specific OS user. However, your Linux should have a few necessary prerequisite packages already present.

Now as a regular user under which we are planning to setup the azure cli, run the following command:

curl -L https://aka.ms/InstallAzureCli | bash

This downloads a script file and executes it on bash. Which in turn downloads and installs many python packages into a sub directory within the user’s home ($HOME/lib/azure-cli) by default. And it places a small executable script file az in $HOME/bin.

I prefer Azure to use the python available in the system rather than bringing its own python and placing it in $HOME/lib/azure-cli/bin/python even though I have a compatible python in my machine. It downloads a lot of python wheel packages and installed into $HOME/lib/azure-cli. Overall it consumed 144MB in my disk.

Setting up Login Credentials

On shell, we can invoke the Azure cli to setup login:

$ az login

This will give an output to screen something like:

To sign in, use a web browser to open the page https://aka.ms/devicelogin 
and enter the code HNXXXXXXN to authenticate.

Opening the url mentioned in a web browser leads to a screen like:

Once we key in (or copy & paste) the code as mentioned in the screen

it is taking us to Azure login screen for final validation

On the command line, it displays a json message with your credential details.

    "cloudName": "AzureCloud",
    "id": "208cxxxx-xxxx-xxxx-xxxx-d1xxxxe9a4ef",
    "isDefault": true,
    "name": "Free Trial",
    "state": "Enabled",
    "tenantId": "044dxxxx-xxxx-xxxx-xxxx-0xxxxbf8xxxc",
    "user": {
    "name": "jobin.augustine@openscg.com",
    "type": "user"

We don’t have to store or remember anything from this, Azure client automatically stores the details for us.

Listing Postgres Logs

First attempt is to list the available logs. By default it will list the log files for the last 72 hours.

$ az postgres server-logs list --resource-group JobinResource --server jobintestDB

This produces an array of json like

    "id": "/subscriptions/.../postgresql-2018-01-11_114851.log",
    "lastModifiedTime": "2018-01-11T11:49:06+00:00",
    "logFileName": "postgresql-2018-01-11_114851.log",
    "logFileType": "text",
    "name": "postgresql-2018-01-11_114851.log",
    "resourceGroup": "JobinResource",
    "sizeInKb": 1,
    "type": "Microsoft.DBforPostgreSQL/servers/logFiles",
    "url": "https://wasd2prodince1afse9.file.core.windows.net/….&sp=r"

List command supports additional options like:

–file-last-written 10

This means list only last 10 hours logs

–filename-contains filenamepattern


This is useful for filtering out large logfiles from downloading. Specify the number in Kb.

Downloading log files

By placing the “url” part of the json, we can download the logfile.

Azure CLI provides options for downloading the log files using command line options.

We have to specify name of the logfile which is displayed in the above “list” command

$ az postgres server-logs download --name postgresql-2018-01-11_114851.log 
--resource-group JobinResource --server jobintestDB

if we have to download multiple files in single shot, CLI allows us to specify space separated list of files to download like this:

$ az postgres server-logs download --resource-group JobinResource --server jobintestDB 
-n postgresql-2018-01-11_114851.log postgresql-2018-01-11_120000.log

Automating downloads

I want to analyze log files daily by downloading the last 24 hours log files. Unfortunately I don’t see a single CLI execution posible. But we can achieve the same by combining 2 CLI execution and directing output of the first one to second one.

$ az postgres server-logs download --resource-group JobinResource 
--server jobintestDB -n `az postgres server-logs list 
--resource-group JobinResource --server jobintestDB --file-last-written 24 --query [*].[name] -
-out table | sed -n '3,$p' | tr '\n' '