DEV Community

Cover image for A Step-by-Step Guide to Connecting Excel to a PostgreSQL Database
Pizofreude
Pizofreude

Posted on

A Step-by-Step Guide to Connecting Excel to a PostgreSQL Database

In today's data-driven world, the ability to seamlessly integrate data from various sources is paramount. Microsoft Excel, a staple tool in many workplaces, can be leveraged to directly connect to a PostgreSQL database, enabling users to access and analyze database information without the need for complex data manipulation. If you’re wondering, current MS365 only support MS SQL Server natively and the rest is unofficially supported by plugins or OBDC driver. In this tutorial, we'll walk through the process of connecting Excel to a PostgreSQL database using the ODBC driver method.

Prerequisites:

  • Microsoft Excel 2019 or an older compatible version.
  • Access to a PostgreSQL database.
  • Postgres ODBC driver installer (compatible with your Excel version).

Step 1: Download and Install the Postgres ODBC Driver

  • Begin by downloading the Postgres ODBC driver from the official PostgreSQL website (ensure compatibility with your Excel version).
  • Follow the installation instructions provided with the driver to complete the installation process on your system.

Step 2: Accessing Excel Data Tab

  • Open Microsoft Excel on your computer.
  • Navigate to the "Data" tab located in the Excel ribbon at the top of the screen.

Step 3: Establishing Connection

  • Within the "Data" tab, locate and click on "Existing Connections" and click “Browse for More”:

Existing Connections

  • Afterwards, select “+Connect to New Data Source.odc” and click “Open”:

Connect to New Data Source

  • Next, choose “Other/Advanced”:

Other/advanced

  • From the dropdown menu, select "Microsoft OLE DB Provider for ODBC Drivers":

ODBC Driver

  • Under “File Data Source” tab, select “New” and choose “PostgreSQL ANSI(x64):

Postgres ANSIx64

To connect specific PostgreSQL database with Excel, we need to specify a name for the file data source (DSN) that we want to save this connection to. This name is essentially an identifier for the ODBC data source we’re creating, which Excel will use to establish a connection to our PostgreSQL database.

Here’s what we can do:

  1. Choose a meaningful name for our DSN, such as PostgreSQL_DB_CyclingSuperstores.
  2. Enter this name in the field where it asks for the name of the file data source.
  3. Alternatively, click “Browse” to select a location on our computer where we want to save the DSN file, and then provide a name for the file.

Once we have named and saved our DSN, we can proceed with the rest of the connection setup in Excel. Remember, the DSN name should be unique and descriptive enough to identify the specific PostgreSQL database connection you are configuring.

Step 4: Configuring Connection Settings

  • A dialogue box will appear prompting us to enter connection details. Fill in the required information:
    • Username: Your PostgreSQL username. By default: postgres
    • Password: Your PostgreSQL password.
    • Database: The name of the PostgreSQL database we wish to connect to.
    • IP Address/Server: Enter the IP address or hostname of our PostgreSQL server e.g. localhost .
    • Data Source Name: Provide a name for your data source.

Data Source Name

Step 5: Finalizing Connection

  • Once all connection details are entered, click "OK" to establish the connection between Excel and the PostgreSQL database.
  • If the connection is successful, Excel will notify you, and you will now have access to the PostgreSQL database within Excel.
  • For example:

Example 1

Step 6: Browsing and Selecting Data

  • With the connection established, you can now browse and select the tables or data you wish to import into Excel.
  • Navigate through the available tables in the database and select the desired data for import.

For example, in our Data Series blog, we can select cc_dashboard as the dataset to be imported:

Example 2

Congrats! We have successfully connected Microsoft Excel to a PostgreSQL database using the ODBC driver method. We can now leverage Excel's powerful features to analyze, visualize, and manipulate data directly from our PostgreSQL database.

By following these simple steps, we can streamline our data workflow and harness the full potential of both Microsoft Excel and PostgreSQL for enhanced data analysis and decision-making.

Top comments (0)