xorsyst
xorsyst

Reputation: 8257

How to read postgresql dump file in Python

I'd like my Python script to read some data out of a postgresql dump file. The Python will be running on a system without postgresql, and needs to process the data in a dump file.

It looks fairly straightforward to parse the CREATE TABLE calls to find the column names, then the INSERT INTO rows to build the contents. But I'm sure there would be quite a few gotchas in doing this reliably. Does anyone know of a module which will do this?

Upvotes: 8

Views: 9810

Answers (2)

bitinerant
bitinerant

Reputation: 1571

So ... 7 years later, there is now a Python package written specifically for this:

pip install pgdumplib

From the PyPI page, pgdumplib is a "Python3 library for reading and writing pg_dump files using the custom format."

Upvotes: 6

xorsyst
xorsyst

Reputation: 8257

Thanks for all the comments, even if they are mostly "don't do this!" ;)

Given:

  1. The dump is always produced in the same format from a 3rd-party system
  2. I need to be able to automate reading it on another 3rd-party system without postgres

I've gone for writing my own basic parser, which is doing a good enough job for what I require.

Upvotes: 1

Related Questions