DEV Community

Cover image for Integrating PostgreSQL with Power BI for Data Analysis
Erasto Wamuti
Erasto Wamuti

Posted on

Integrating PostgreSQL with Power BI for Data Analysis

Power BI is a business intelligence tool made for extracting, organizing, and visualizing data in business. The goal is to take raw data and get insights after processing and analysis. Data can be in various forms, such as spreadsheets, databases, cloud-hosted files, and more.

Databases

In the past, organizations stored data in files and folders, but now other forms of data storage are favored. Technological improvements have made applications mainstream in business, and data storage has changed significantly alongside them. The modern business landscape demands real-time data access, concurrent collaboration, and structured storage solutions. The need for database storage cannot be ignored. Databases offer ease of access, security, and concurrency.

Databases are categorized depending on the structure they use to store data:

  • Structured Query Language (SQL): Use relational tables to store information. Examples: MySQL, PostgreSQL.
  • No-SQL (Non-relational): Use other means such as JSON, objects, etc. Examples: Redis, MongoDB.

SQL-based databases are ideal for working with structured data that can be combined through relationships. They enable data cleaning and processing to produce accurate data, which is later used for analysis.

Connecting Power BI to a Local Postgres Database

Postgres is a structured query database and is used to develop modern applications and online systems. As we have seen, there are various data sources such as Excel files, CSV files, database files, cloud storage, etc. Power BI is capable of importing and loading the data from these sources.

Database Connections

Database systems can be hosted on:

  • Local servers: the computer a user is operating (localhost)
  • Online database services: such as AWS, GCP, Aiven, DB Clusters, etc.

companies with online database services

Connecting Power BI to a locally hosted Database

Here are the steps to connecting your Power BI to a locally hosted Postgres database and importing data:

a. Open Power BI and select Get Data on the topbar menu.

get data from sql

b. Scroll through the list of sources and click more options. Select databases and the corresponding PostgreSQL DB on the right and connect.

database connection

c. Enter the server as localhost and the name of the database you are connecting to.

hostname

d. Provide the name and the password to the database.

password

e. Once the connection is successful, Power BI loads all the tables you have created in the database. Select the tables you wish to work on and load the data.

load data

f. You can open the Power query to check for data consistency and correct any errors in the data.

power-query

h. Once the cleaning is done you can create a relationship between the tables using the common column(id) between them. Open the model view and inspect the relationship between the tables.

table-relationships

Connecting Power BI to a Cloud PostgreSQL Database (Aiven)

It is standard practice for online servers that host databases to establish security protocols. As such, to connect Power BI to a database hosted on a cloud service, you have to establish a secure connection. This is achieved by the use of an encryption certificate that encrypts data from your computer, and the server only responds by decrypting the message using the encryption rules. Only the computer that has this certificate can connect to the server.

Part 1: Connect to Database Server

For our case, the Aiven cloud service that hosts our database has all these details.
Follow these steps to connect your Power BI to Aiven:

a. Select the database service in Aiven you wish to connect to and activate it. In this case, the PostgreSQL Database service. Click the Service Overview to display the service details.

aiven dashboard

b. Click the Download button on the CA Certificate and save it to your PC.

c. Rename the downloaded file from ca.pem to ca.crt.
d.Double-click the ca.crt file and click Install Certificate on your PC.

install ca certificate

e. Select Local Machine as the installation location.

location on pc-machine

f.Select the certificate store: choose Trusted Root Certification Authorities.

certificate-store
g. Click Finish.The setup is complete once the import is successful.

Part 2: Login to the Database

a. Gather the following details from the Aiven Overview Page:

  • Host Name
  • Port
  • Database Name
  • Username
  • Password

b. Open Power BI and click Get Data.

get-data

c. In the list, select More and choose PostgreSQL Database.
postgres-database

d. Enter the Server Name and Database Name using the values copied from Aiven. Leave the data connectivity mode on the default Import option.

Note: The server name uses the format hostname:port_number.

e. Insert the server name as hostname and name of the database, then click OK.

server-name

f. Insert the username and password, then click Connect.

uname-password

Loading Tables and Creating Relationships in Power BI

A successful connection displays the tables stored in the database. In this case: Customers, Products, Sales, and Inventory.

a. Select the tables you require and load them into Power BI.

data-tables

b. Open Power Query and inspect the data. Clean inconsistent data, address any duplicates and null values, then save changes.

power-query

c. Open the Model View and create relationships between tables using common columns, typically primary and foreign keys. In some cases, Power BI selects relationships automatically. Review and delete any unwanted connections.

data-models

What is Data Modeling?

Data modeling is the process of defining how data is stored, structured, and related within a database or analytics tool like Power BI. Think of it as creating a blueprint for your data so that different pieces of information can talk to each other.

Why Relationships Matter

In a well-designed system, data is split into multiple tables: Customers, Inventory, Sales, and Products to avoid repetition and keep things organized. Relationships are the bridges that connect these tables using common fields, such as a Product ID. Without these connections, data exists in isolated containers.

Relationships allow you to:

  • Combine Data: See the name of a product next to its sales figures.
  • Analyze Correctly:When you filter by category, sales numbers update to show only that category.
  • Support Accurate Reporting: Prevent double-counting or mismatched information that leads to incorrect business decisions.

In Power BI, the data model is what makes your dashboards interactive. When you click a slice of a pie chart, the rest of the report updates because of the underlying relationships.

Types of Relationships

Once you've identified the connecting field, you must define the direction and cardinality of the relationship. The three most common types are:

Type Description Example
One-to-Many The gold standard one record relates to many One product can be sold many times
One-to-One Used for splitting large tables Employees and Employee Sensitive Details
Many-to-Many Complex multiple items on both sides relate Multiple products in multiple orders

Conclusion

SQL (Structured Query Language) is often considered a handy tool for high-level Power BI work. While Power BI has great built-in tools for connecting to data, SQL allows you to handle the heavy lifting before the data even reaches your report.

If Power BI is the kitchen where you cook the meal, SQL is the prep station where you wash, cut, and organize the ingredients.

SQL does this by:

  • Precise Retrieval: Instead of importing a massive table with 100 columns you don't need, you can write a SELECT statement to bring in only the specific columns required.

  • Efficient Filtering: Using a WHERE clause in SQL filters the data at the source (the database). This is much faster than importing millions of rows and filtering them inside Power BI.

  • Pre-Aggregated Data: You can use GROUP BY to sum up sales or average scores in the database. This creates a smaller dataset that makes your Power BI dashboards more responsive.

  • Data Transformation: SQL is powerful for cleaning messy data, renaming columns, handling null values, or combining strings before the data model is even built.

Top comments (0)