DEV Community

Edmund Eryuba
Edmund Eryuba

Posted on

Connecting Power BI to a SQL Database (PostgreSQL)

Introduction

Power Query is a data transformation and data preparation engine. Power Query comes with a graphical interface for getting data from sources and a Power Query editor for applying transformations. Because the engine is available in many products and services, the destination where the data is stored depends on where Power Query is used. Using Power Query, you can perform the extract, transform, and load (ETL) processing of data.

Microsoft Power BI is a comprehensive Business intelligence platform that uses Power Query to ingest data, then adds modelling, visualization, and sharing capabilities. Power Query preps the data, while Power BI turns it into reports

Power Query

Why use PostgreSQL?

Maintaining dynamic database systems is critical in today’s digital landscape, especially considering the rate in which newer technologies emerge. PostgreSQL is expandable and versatile so it can quickly support a variety of specialized use cases with powerful extension ecosystem, which covers things from time-series data types to geospatial analytics.

Its versatile and approachable design makes PostgreSQL a “one-size-fits-all” solution for many enterprises looking for cost-effective and efficient ways to improve their database management systems.

Built as an open-source database solution, PostgreSQL is completely free from licensing restrictions, vendor lock-in potential, or the risk of over-deployment.

Expert developers and commercial enterprises who understand the limitations of traditional database systems heavily support PostgreSQL. They work diligently to provide a battle-tested, best-of-breed relational database management system.

How Power Query helps with data acquisition

Business users spend up to 80 percent of their time on data preparation, which delays the work of analysis and decision-making. Several challenges contribute to this situation and Power Query helps address many of them.

  1. Enables wide range connectivity of data sources, including data of all sizes and shapes.
  2. Consistency of experience, and parity of query capabilities over all data sources.
  3. Highly interactive and intuitive experience for rapidly and iteratively building queries over any data source, of any size.
  4. When using Power Query to access and transform data, you define a repeatable process (query) that can be easily refreshed in the future to get up-to-date data.
  5. Power Query offers the ability to work against a subset of the entire data set to define the required data transformations, allowing you to easily filter down and transform your data to a manageable size.

Power Query experiences

The Power Query user experience is provided through the Power Query editor user interface. The goal of this interface is to help you apply the transformations you need simply by interacting with a user-friendly set of ribbons, menus, buttons, and other interactive components.

The Power Query editor is the primary data preparation experience. In the editor, you can connect to a wide range of data sources and apply hundreds of different data transformations by previewing data and selecting transformations from the UI. These data transformation capabilities are common across all data sources, whatever the underlying data source limitations.

When you create a new transformation step by interacting with the components of the Power Query interface, Power Query automatically creates the M code required to do the transformation so you don't need to write any code.

Connecting Power BI to a Local PostgreSQL Database

Connecting Microsoft Power BI to an SQL database allows you to import data and build dashboards directly from your database tables. The exact steps depend slightly on the SQL engine (e.g., PostgreSQL, MySQL, or Microsoft SQL Server), but the workflow is mostly the same.

1. Get data in Power BI Desktop

Launch Power BI Desktop. On the Home screen you will see options for selecting a data source or start with a blank report. Click on the Blank report option to be directed to the Home tab.

Blank Report

In Power BI Desktop, you can directly select an Excel worksheet, a Power BI semantic model, a SQL server database, direct data entry, Dataverse data or recently used data source without using the Get data option.

From the Data ribbon, selecting Get Data provides additional methods to select the desired connector.

Get

Select the More option which opens a Get Data window that contains a complete list of available connectors.

2. Connection settings

After clicking Get Data: Choose Database > Select PostgreSQL Database > Click Connect

Power BI includes a built-in PostgreSQL connector that allows direct communication with PostgreSQL servers.

Scroll to PostgeSQL database, select the option and click on Connect to close the window.

Get Data

In the PostgreSQL database dialog that appears, provide the name of the server and database.

PgSQL

Select either the Import or DirectQuery data connectivity mode. Use Import for snapshots or DirectQuery for live data.

Power BI allows you to optionally include a SQL query in the Advanced Options section if you want to retrieve only specific data from the database.

3. Authentication Credentials

If you're connecting to this database for the first time, select the authentication type you want to use, and then enter your credentials. The authentication types available are:

  • Database (Username and password)
  • Microsoft account (Microsoft Entra ID)

Authenticate

These credentials must match the PostgreSQL database user account.

Once authenticated, Power BI establishes a connection to the PostgreSQL server and retrieves metadata about the available tables.

Data preview

The goal of the data preview stage is to provide you with a user-friendly way to preview and select your data.

The Navigator lists all tables in the database, allowing one to preview each table, select multiple tables or load them directly. Either select Load to load the data or Transform Data to continue transforming the data in Power Query editor.

The data preview pane on the right side of the window shows a preview of the data from the object you selected.

Connecting Power BI to a Cloud PostgreSQL Database (Aiven)

Organizations often host databases in the cloud rather than on local machines. One example is Aiven, which provides managed PostgreSQL services.

Connecting Power BI to a cloud PostgreSQL database is similar to connecting to a local database, but additional security settings are required.

1. Obtain Connection Details from Aiven

Get Connection Details: Log in to your Aiven Web Console and navigate to your PostgreSQL service to find the Host, Port, Database Name, and Username/Password.

Aiven

2. Download the SSL Certificate

Most cloud database providers require encrypted connections for security reasons. Aiven provides an SSL certificate that ensures secure communication between Power BI and the database.

From the Aiven console:

  1. Navigate to Connection Information
  2. Download the CA Certificate
  3. Save the certificate file on your computer

SSL encryption ensures:

  • Data transmitted over the internet cannot be intercepted
  • Authentication of the database server
  • Secure communication between the client and server

3. Connect Using Power BI

Open Power BI: Open Power BI Desktop, click Get Data on the Home ribbon, and select More....

More

Select connector: Choose Database > PostgreSQL database and click Connect.

Datag

Enter Credentials: Provide the server and database name, your Aiven username and password and enable SSL if required. Power BI will use the certificate to verify the database server and establish a secure connection.

pgsql db

Loading Data and Creating Relationships

After connecting to the database, the tables are loaded into Power BI’s data model.

Power BI automatically detects relationships based on matching column names such as customer_id or product_id.
However, relationships can also be created manually.

These relationships form a data model, which defines how tables interact with each other.

Summary

Power BI is a powerful business intelligence platform that allows organizations to transform raw data into meaningful insights. By connecting Power BI to SQL databases such as PostgreSQL, companies can access large datasets, build interactive dashboards and make data-driven decisions.

Connecting to a local PostgreSQL database involves selecting the PostgreSQL connector in Power BI, entering the server and database details, authenticating with credentials, and loading tables into the Power BI model. When connecting to cloud databases such as those hosted on Aiven, additional security measures such as SSL certificates ensure that the connection is encrypted and secure.

Once the data is loaded, Power BI allows analysts to create relationships between tables, forming a structured data model that supports accurate analysis. Strong SQL skills further enhance a Power BI analyst’s ability to retrieve, filter, aggregate, and prepare data efficiently before building reports.

Top comments (0)