Import Multiple PostgreSQL Tables Into Excel Software — Fast & Automated

How to Import Multiple PostgreSQL Tables Into Excel — Step-by-step

Overview (assumption)

Assume you have access to the PostgreSQL server, credentials, and Excel on Windows or macOS. This guide shows two reliable approaches: exporting CSVs with psql/pgAdmin (best for many tables) and connecting Excel directly via ODBC/Power Query (best for live refresh).


Option A — Export multiple tables to CSV then open in Excel (recommended for bulk/export)

  1. List tables (example SQL to run in psql or pgAdmin Query Tool):

    sql

    SELECT table_schema, table_name FROM information_schema.tables WHERE table_type=‘BASE TABLE’ AND table_schema NOT IN (‘pg_catalog’,‘informationschema’);
  2. Create export folder on the machine that can access PostgreSQL server (e.g., C:\exports).
  3. Export each table to CSV (server-side COPY or client-side psql \copy). Example using psql client from your workstation (replace placeholders):

    bash

    # loop in shell (Linux/macOS) for t in \((</span><span class="token" style="color: rgb(54, 172, 170);">psql -t -c </span><span class="token" style="color: rgb(163, 21, 21);">"SELECT table_schema||'.'||table_name FROM information_schema.tables WHERE ..."</span><span class="token" style="color: rgb(54, 172, 170);">)</span><span class="token" style="color: rgb(57, 58, 52);">;</span><span> </span><span class="token" style="color: rgb(0, 0, 255);">do</span><span> </span><span></span><span class="token assign-left" style="color: rgb(54, 172, 170);">schema</span><span class="token" style="color: rgb(57, 58, 52);">=</span><span class="token" style="color: rgb(54, 172, 170);">\)(echo \(t </span><span class="token" style="color: rgb(57, 58, 52);">|</span><span class="token" style="color: rgb(54, 172, 170);"> </span><span class="token" style="color: rgb(57, 58, 52);">cut</span><span class="token" style="color: rgb(54, 172, 170);"> -d. -f1</span><span class="token" style="color: rgb(54, 172, 170);">)</span><span> </span><span> </span><span class="token assign-left" style="color: rgb(54, 172, 170);">table</span><span class="token" style="color: rgb(57, 58, 52);">=</span><span class="token" style="color: rgb(54, 172, 170);">\)(echo \(t </span><span class="token" style="color: rgb(57, 58, 52);">|</span><span class="token" style="color: rgb(54, 172, 170);"> </span><span class="token" style="color: rgb(57, 58, 52);">cut</span><span class="token" style="color: rgb(54, 172, 170);"> -d. -f2</span><span class="token" style="color: rgb(54, 172, 170);">)</span><span> </span><span> psql -c </span><span class="token" style="color: rgb(163, 21, 21);">"</span><span class="token" style="color: rgb(255, 0, 0);">\c</span><span class="token" style="color: rgb(163, 21, 21);">opy </span><span class="token" style="color: rgb(54, 172, 170);">\){schema}.\({table}</span><span class="token" style="color: rgb(163, 21, 21);"> TO 'C:/exports/</span><span class="token" style="color: rgb(54, 172, 170);">\){schema}${table}.csv’ DELIMITER ‘,’ CSV HEADER” done

    Or single-table server-side:

    sql

    COPY public.mytable TO ’/var/lib/postgresql/exports/mytable.csv’ DELIMITER ’,’ CSV HEADER;
  4. Transfer CSVs to the machine with Excel if exported on the server.
  5. Open in Excel:
    • Excel: Data → Get Data → From Text/CSV → select CSV → Load. Repeat per file or use Power Query to combine.
  6. Optional: Combine multiple CSVs into a single workbook:
    • In Excel: Data → Get Data → From File → From Folder → point to export folder → Combine & Load. This creates tables/sheets per file or combined table.

Option B — Connect Excel to PostgreSQL for multiple tables (live queries, refreshable)

  1. Install PostgreSQL ODBC driver (psqlODBC matching Excel bitness).
  2. Create ODBC DSN in Windows ODBC Data Source Administrator (User/System DSN) with host, port, DB, user, password.
  3. In Excel (Power Query):
    • Data → Get Data → From Other Sources → From ODBC (or From Database → From PostgreSQL if available).
    • Choose DSN, then Navigator shows available schemas/tables. Select multiple tables and Load or Transform Data.
  4. Configure refresh: Right-click query/table → Properties → enable background refresh and refresh on open.

Tips & troubleshooting

  • Permissions: Ensure DB user has SELECT and (for server-side COPY) file write permissions.
  • Large tables: Use COPY/psql \copy for speed; consider splitting or sampling for Excel size limits.
  • Data types: Dates/numerics usually map cleanly; export text containing commas/line breaks with CSV HEADER and proper quoting.
  • Delimiter/encoding: Use UTF-8 and DELIMITER ‘,’; if commas appear in data, CSV quoting is required.
  • Schema names: Use schema.table to avoid ambiguous names.
  • Automation: Script exports (cron/Task Scheduler) or use Power Query refresh for recurring jobs.

If you want, I can generate: a) a ready-made shell/psql script for your specific tables, or b) step-by-step ODBC setup instructions for Windows or macOS — tell me which.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *