DEV Community

Cover image for Connecting Power BI to a SQL Database: A Complete Guide
Jill Nandaha
Jill Nandaha

Posted on

Connecting Power BI to a SQL Database: A Complete Guide

Introduction: Power BI and the Role of SQL Databases

In today's data-driven world, businesses generate enormous amounts of information every single day — from sales transactions to customer interactions, inventory movements, and beyond. Making sense of all that data requires the right tools. Microsoft Power BI is one of the most widely used business intelligence platforms in the world, and for good reason.

Power BI is a data visualization and analytics tool that allows analysts, data professionals, and business users to connect to data sources, transform raw data, build interactive dashboards, and share insights across an organization. With drag-and-drop simplicity and powerful underlying query capabilities, it enables even non-technical stakeholders to explore trends, track KPIs, and make data-backed decisions in real time.

But Power BI is only as good as the data it connects to. That's where SQL databases come in.

SQL (Structured Query Language) databases — such as PostgreSQL, MySQL, and Microsoft SQL Server — are the backbone of most modern data infrastructure. They store structured, relational data in an organized, queryable format. Companies rely on SQL databases because they are reliable, scalable, and optimized for both storing and retrieving large volumes of analytical data.

When companies connect Power BI to a SQL database, they unlock the ability to build live, always-updated dashboards powered by real production data — no more manually exported spreadsheets, no more stale reports. The combination of SQL's data management strengths and Power BI's visualization capabilities is one of the most powerful setups in modern business intelligence.

In this article, you'll learn:

  • How to connect Power BI to a local PostgreSQL database
  • How to connect to a cloud PostgreSQL database on Aiven
  • How to load and model your tables in Power BI
  • Why SQL skills matter even when using a visual tool like Power BI

Part 1: Connecting Power BI to a Local PostgreSQL Database

Prerequisites

Before you begin, make sure you have:

  • Power BI Desktop installed (download free from powerbi.microsoft.com)
  • PostgreSQL installed and running locally
  • A database with tables already created (e.g., customers, products, sales, inventory)
  • The PostgreSQL ODBC driver or the Npgsql connector installed

Step 1: Open Power BI Desktop

Launch Power BI Desktop. You'll land on the Home screen showing recent reports and quick-start options.

Screenshot: Power BI Desktop home screen

Power BI Desktop home screen

Step 2: Click "Get Data"

On the Home ribbon at the top, click the Get Data button. A dropdown will appear with common data source categories.

Screenshot: "Get Data" button highlighted on the Home ribbon

Step 3: Select "PostgreSQL Database"

In the Get Data dialog box, either search for "PostgreSQL" in the search bar, or navigate to the Database category on the left panel. Select PostgreSQL Database and click Connect.

Screenshot: Get Data dialog with "PostgreSQL Database" selected

Get Data dialog with

Step 4: Enter Server and Database Details

A connection dialog will appear asking for two fields:

Field What to Enter
Server localhost (for a local database)
Database The name of your PostgreSQL database (e.g., sales_db)

You can also expand Advanced Options to write a custom SQL query if you don't want to load entire tables.

Screenshot: PostgreSQL connection dialog showing the Server and Database fields

Click OK to proceed.

PostgreSQL connection dialog showing the Server and Database fields

Step 5: Enter Your Credentials

Power BI will prompt you for authentication. Choose Database authentication, then enter your PostgreSQL username and password.

Screenshot: Credentials dialog with Database authentication selected

Click Connect.

Credentials dialog with Database authentication selected

Step 6: Select Tables to Load

The Navigator window will open, showing all available schemas and tables in your database. Check the boxes next to the tables you want to load — for example: customers, products, sales, and inventory.

You'll see a preview of the selected table's data on the right side.

Screenshot: Navigator pane showing table list with checkboxes and a data preview

Click Load to import the tables directly, or Transform Data to open the Power Query editor for data cleaning first.

Navigator pane showing table list with checkboxes and a data preview

Part 2: Connecting Power BI to a Cloud PostgreSQL Database (Aiven)

Many organizations don't run databases on local machines — they use cloud-hosted databases for scalability and availability. Aiven is a popular managed cloud database service that supports PostgreSQL. Connecting to it from Power BI requires a few extra steps, particularly around SSL security.


Step 1: Get Your Connection Details from Aiven

Log in to your Aiven Console. Navigate to your PostgreSQL service. On the Overview tab, you'll find all the connection details you need:

Detail Example Value
Host pg-yourservice.aivencloud.com
Port 12345
Database defaultdb
Username avnadmin
Password (shown in the console)

Screenshot: Aiven console showing connection details for a PostgreSQL service

Aiven console showing connection details for a PostgreSQL service

Step 2: Download the SSL Certificate

Still on the Aiven Overview page, scroll down to the Connection Information section. Click Download CA Certificate to download the ca.pem file. Save it somewhere accessible on your machine (e.g., C:\certs\ca.pem).

Screenshot: Aiven "Download CA Certificate" button

Aiven

Why Is an SSL Certificate Required?

When connecting to a cloud database over the internet, your credentials and data travel across public networks. An SSL (Secure Sockets Layer) certificate encrypts this connection, ensuring that:

  • Your data cannot be intercepted by third parties (man-in-the-middle attacks)
  • The server you're connecting to is verified and legitimate
  • Your username and password are transmitted securely

Aiven enforces SSL by default on all connections, which is a security best practice.


Step 3: Connect in Power BI

Follow the same steps as the local connection:

  1. Click Get Data → PostgreSQL Database
  2. In the Server field, enter your Aiven host:port (e.g., pg-yourservice.aivencloud.com:12345)
  3. Enter your database name
  4. Expand Advanced Options and paste the following into the Additional Connection Parameters field:
sslmode=require;sslrootcert=C:\certs\ca.pem
Enter fullscreen mode Exit fullscreen mode

Screenshot: PostgreSQL connection dialog with Aiven host and SSL parameters filled in

PostgreSQL connection dialog with Aiven host and SSL parameters filled in

  1. Enter your Aiven username and password when prompted
  2. Click Connect

Once authenticated, the Navigator will appear just like with a local database — select your tables and load them in.


Part 3: Loading Tables and Building Relationships

Loading the Tables

After clicking Load in the Navigator, Power BI will import your selected tables. In this case, we're working with four tables:

  • customers — customer profiles and contact information
  • products — product catalog with pricing and categories
  • sales — transactional records linking customers to products
  • inventory — stock levels per product

Once loaded, these tables appear in the Data pane on the right side of Power BI Desktop.

Screenshot: Power BI Data pane showing the four loaded tables

Power BI Data pane showing the four loaded tables

Creating Relationships in the Model View

Click the Model icon on the left sidebar (it looks like a diagram with connected boxes). Power BI often auto-detects relationships, but you should always verify them manually.

Here's how the relationships between our four tables should look:

customers ──────────── sales ──────────── products
  (customer_id)    (customer_id, product_id)  (product_id)
                          │
                     inventory
                     (product_id)
Enter fullscreen mode Exit fullscreen mode

To create or edit a relationship:

  1. Drag a field from one table onto the matching field in another (e.g., drag customer_id from customers to sales)
  2. Power BI will draw a line representing the relationship
  3. Double-click the line to set the Cardinality (e.g., One-to-Many) and Cross-filter direction

Screenshot: Model view showing relationships between customers, sales, products, and inventory

Model view showing relationships between customers, sales, products, and inventory

Why Relationships Matter: A Note on Data Modeling

Data modeling is the process of defining how tables relate to one another so that the BI tool can correctly aggregate and filter data. Without relationships:

  • A sales report can't look up a customer's name from the customers table
  • A product performance chart can't match sales figures to product categories
  • Filters applied to one table won't propagate correctly to related visuals

With properly defined relationships, Power BI's engine knows how to join tables automatically whenever you drag fields from different tables into the same visual — no manual SQL joins required on your end.

The key concept here is the star schema model: one central fact table (sales) surrounded by dimension tables (customers, products, inventory). This structure is optimized for analytics and is the standard data modeling pattern in Power BI.

Screenshot: Example of a completed star schema in Power BI model view

Example of a completed star schema in Power BI model view

Part 4: Why SQL Skills Matter for Power BI Analysts

Power BI handles a lot automatically — it connects to databases, creates visuals, and even suggests relationships. So why does a Power BI analyst need to know SQL?

The answer is: because Power BI is a presentation layer, not a data preparation layer. The real work often happens before a single chart is built.

1. Retrieving Only What You Need

Loading an entire table with millions of rows into Power BI slows everything down. With SQL, you can write a query that pulls only the columns and rows relevant to your analysis:

SELECT customer_id, name, region, signup_date
FROM customers
WHERE signup_date >= '2023-01-01';
Enter fullscreen mode Exit fullscreen mode

In Power BI's Get Data → Advanced Options, you can paste this query directly instead of loading the full table.


2. Filtering Datasets Before Import

Rather than filtering data inside Power BI (which still loads the full table first), SQL lets you filter at the source — much more efficient:

SELECT *
FROM sales
WHERE sale_date BETWEEN '2024-01-01' AND '2024-12-31'
  AND status = 'completed';
Enter fullscreen mode Exit fullscreen mode

3. Performing Aggregations at the Database Level

Aggregating millions of rows in SQL before sending data to Power BI is faster than letting Power BI do it after loading everything:

SELECT
    p.category,
    SUM(s.quantity * s.unit_price) AS total_revenue,
    COUNT(DISTINCT s.customer_id) AS unique_customers
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.category
ORDER BY total_revenue DESC;
Enter fullscreen mode Exit fullscreen mode

This kind of pre-aggregated table is perfect to load into Power BI as a summary dataset.


4. Preparing and Cleaning Data

Real-world data is messy. SQL lets you handle duplicates, fill nulls, standardize formats, and combine tables before they ever reach Power BI:

SELECT
    customer_id,
    UPPER(TRIM(name)) AS name,
    COALESCE(email, 'no-email@unknown.com') AS email,
    region
FROM customers
WHERE customer_id IS NOT NULL;
Enter fullscreen mode Exit fullscreen mode

The Bottom Line

SQL gives Power BI analysts control, speed, and precision over their data pipeline. A developer who understands both SQL and Power BI can build dashboards that are not only visually compelling but also performant, accurate, and scalable.


Conclusion

Connecting Power BI to a SQL database — whether local or cloud-hosted — is a foundational skill for anyone working in data analytics or business intelligence. In this article, we walked through the full process: from clicking Get Data in Power BI Desktop, to configuring secure SSL connections for cloud databases like Aiven, to loading and modeling tables for accurate analysis.

We also saw that SQL knowledge isn't just a "nice to have" — it's what separates analysts who build fast, clean dashboards from those constantly fighting performance issues and data quality problems.

As you build more complex Power BI reports, you'll find yourself reaching for SQL more and more — not because Power BI can't handle things on its own, but because the best analysts know when to push work to the database and when to let the visualization tool take over.

Happy building!

Top comments (0)