Power Query PostgreSQL: Connect and Load Data
In this article
You connect Power Query to PostgreSQL by installing the Npgsql data provider, then pointing Power Query at the server and database with File > Get Data > Database > PostgreSQL database in Excel or Power BI. Power Query for Excel and Power BI Desktop has a built-in PostgreSQL connector, but it relies on the free Npgsql driver being installed on the same machine, so the very first attempt often fails with a provider error if the driver is missing. Once the connection works, you choose the tables or write a native SQL query, shape the data with the usual steps, and load it to a table, the data model or a report.
What You'll Need
- Power Query in Excel (Data > Get Data) or Power BI Desktop (Home > Get data). The labels and layout differ slightly by version and by desktop vs. the Power BI service, so your menus may read a little differently.
- The Npgsql driver installed on the same computer that runs Power Query. On Windows, get the official installer from the Npgsql project. This is a Windows component; on macOS and Linux the driver story differs, so check the Npgsql documentation for your platform.
- Connection details from your database administrator: host name or IP, port (PostgreSQL's default is 5432), database name, and a username and password.
- Network access to the server. A database that only accepts connections from inside a private network will not respond from a laptop on a home connection unless a VPN or firewall rule is in place.
- The server must allow the authentication method your account uses and accept connections from your client's IP address; this is set in PostgreSQL's pg_hba.conf and is the most common reason a correct credential still fails.
Step-by-Step
- Install the Npgsql driver first. Download the official Npgsql installer and run it on the machine where Power Query runs. Close and reopen Excel or Power BI Desktop afterwards so the new driver is picked up. If you skip this, later steps fail with a message that the provider is not registered.
- Start the connector. In Excel, go to Data > Get Data > From Database > From PostgreSQL database. In Power BI Desktop, use Home > Get data and choose PostgreSQL database. Menus vary slightly by version, so look for the PostgreSQL entry under the database sources.
- Enter the server and database. Type the host name (and port if it is not 5432, as
server:port) and the database name. Choose the data connectivity mode: Import loads a copy into the workbook or model, while DirectQuery leaves the data in PostgreSQL and queries it live. Import is the safer default for most work; DirectQuery has limits on which transformations are supported. - Pick the authentication method. At the credentials prompt, sign in with the database username and password. For PostgreSQL, use the Database tab rather than the Windows tab unless your administrator has explicitly set up integrated authentication. Do not choose anonymous or the wrong tab, or the server will reject the login.
- Choose tables or write SQL. The Navigator window lists schemas and tables. Tick the tables you want and use Preview to sanity-check columns. For anything complex, open the advanced options in the connection dialog and paste a native query, for example
SELECT * FROM sales.orders WHERE order_date >= CURRENT_DATE - INTERVAL '90 days'. A native query keeps the heavy filtering on the server and returns fewer rows. - Shape the data in Power Query Editor. Click Transform Data to open the editor. Set correct data types on every column you rely on (dates, whole numbers, decimals), remove unneeded columns, filter rows and rename columns. PostgreSQL type mapping is usually good, but timestamp with time zone values and text that looks numeric benefit from an explicit type step.
- Close and load. Choose Close & Load (or Close & Apply in Power BI). In Excel, decide whether the result goes to a table on a worksheet, the Data Model, or only a connection. In Power BI, the result goes into the model and can be used in visuals.
- Refresh to confirm. Use Refresh All (Excel) or Refresh (Power BI) once to prove the credentials were stored and the server is still reachable. If refresh prompts for credentials again, check the data source settings and make sure the permission is set to a persistent level rather than one-time.
Common Mistakes
- Skipping Npgsql. The connector exists, but it is not shipped with the app. Installing only the app and expecting PostgreSQL to work is the single most common error.
- Using the Windows credentials tab. PostgreSQL's built-in users are not Windows accounts. Unless your admin set up GSSAPI or LDAP integrated logins, you need the Database tab.
- Loading whole tables when a query would do. Pulling a million-row table into Excel is slow and can hit size limits. Filter or use a native query to cut rows and columns before they reach Power Query.
- Ignoring data types. A date column left as text will not sort or filter by date correctly, and decimals imported as text break aggregations.
- Assuming DirectQuery folds everything. Some transformations run in the engine and some must run locally; local steps on large DirectQuery tables are slow or blocked.
- Hard-coding a password in a shared file. Credentials belong in the data source settings, not in visible query steps. Anyone who shares the file inherits whatever access the refresh account has.
Tips
- Give each query a clear name in the editor; the query name becomes the table name in your model and saves confusion later.
- Use a dedicated read-only database role for reporting connections. It limits damage from a mistaken query and makes permissions easier to audit.
- Keep the number of steps modest and ordered logically; the applied steps pane is your documentation, so rename steps that matter.
- For scheduled refresh in Power BI, the data source must be reachable from the service. A database on a private network needs an on-premises data gateway. Personal gateway covers a single machine; enterprise gateway is the shared option.
- Test a native query in a SQL client first. It is far faster to debug syntax there than inside a refresh failure.
- Check row counts after load. A silent WHERE clause that filters everything is easy to spot and easy to miss.
Troubleshooting
If Power Query cannot find the provider, reinstall Npgsql, restart the app, and confirm the installer architecture matches your Office or Power BI installation. If you get a login failure, verify the username and database, try the same credentials in a SQL client, and ask your admin to check pg_hba.conf and password encryption settings. If the connection times out, test the host and port with a network tool and confirm firewall rules; a VPN may be needed. If a table looks empty, confirm the schema is correct, since the Navigator groups objects by schema and a table in another schema will not appear at the top. If refresh works on your machine but not in the Power BI service, that points to a gateway or credential problem, not the query itself. If the driver fails after an Office update, reinstall the matching Npgsql version and restart.
Frequently Asked Questions
Do I need to install anything besides Excel or Power BI?
Yes. The PostgreSQL connector needs the Npgsql driver installed on the same computer. Without it, you get a provider error before you can enter credentials.
Should I use Import or DirectQuery?
Use Import unless you need live data or the dataset is too large to copy. Import supports the full set of transformations and refreshes on a schedule; DirectQuery queries the server each time and supports fewer transformations.
Can I run my own SQL instead of selecting tables?
Yes. Use the advanced options in the connection dialog to supply a native query. This is the best way to filter and aggregate on the server before data reaches your workbook.
Why does my refresh fail after it worked once?
Usually the stored credentials were one-time or the server password changed. Edit the data source credentials and choose a persistent permission level, then refresh again.
Can the Power BI service refresh from PostgreSQL directly?
Only if the service can reach the database, which normally means an on-premises data gateway when the server sits on a private network. Cloud-hosted databases reachable over the internet can be configured without a gateway.
Does Power Query write data back to PostgreSQL?
No. It reads data. Writing rows back requires a separate process or tool, so treat the connection as read-only in your workflow.