llama_index icon indicating copy to clipboard operation
llama_index copied to clipboard

Add reader for PostgreSQL Databases

Open kevinqz opened this issue 2 years ago • 2 comments

Hey all,

I have:

  • Added a reader to connect and query PostgreSQL Databases.
  • Included an example (.py file) of how to connect and query a database correctly.
  • Updated the docs with the additional PostgreSQL option.

Best, Kevin Saltarelli

kevinqz avatar Jan 22 '23 00:01 kevinqz

thanks for doing this @kevinqz! any chance you could take a look at using the SQLDatabase class instead? https://gpt-index.readthedocs.io/en/latest/reference/struct_store.html - this is already used for our SQL index, and we could use it as a reader too (SQLAlchemy should allow for connecting to most structured databases including postgres)

jerryjliu avatar Jan 22 '23 01:01 jerryjliu

Hi, @jerryjliu! Just added another commit in which I've:

  • Added a DatabaseReader to connect to and query SQL Databases by using SQLAlchemy and the SQLDatabase class.
  • Included an example (now with a Jupyter Notebook .ipynb file).
  • Updated the docs with the additional DatabaseReader option.

Tried a few tests here and everything should be fine to go. Feel free to let me know if anything.

kevinqz avatar Jan 22 '23 06:01 kevinqz

I see your point, @jerryjliu! I've made some changes:

  • Made changes in the way to instantiate the DatabaseReader class. Inherited it as a BaseReader and added an instance variable called sql_database of the SQLDatabase class. Please let me know if I understood and implemented your points correctly.
  • Also added the ability to start from an existing (SQLAlchemy) Engine - not sure if this is something excessive or necessary, please let me know your opinion.
  • Removed 2 old files (related to PostgreSQL only).
  • Added examples of how to instantiate the DatabaseReader class from sql_database (SQLDatabase), an engine (Engine), and an uri (string) in the DatabaseReaderDemo.ipynb file.

Feel free to let me know if I correctly addressed your points, ok? Still new to this whole area. :)

kevinqz avatar Jan 23 '23 05:01 kevinqz

I see your point, @jerryjliu! I've made some changes:

  • Made changes in the way to instantiate the DatabaseReader class. Inherited it as a BaseReader and added an instance variable called sql_database of the SQLDatabase class. Please let me know if I understood and implemented your points correctly.
  • Also added the ability to start from an existing (SQLAlchemy) Engine - not sure if this is something excessive or necessary, please let me know your opinion.
  • Removed 2 old files (related to PostgreSQL only).
  • Added examples of how to instantiate the DatabaseReader class from sql_database (SQLDatabase), an engine (Engine), and an uri (string) in the DatabaseReaderDemo.ipynb file.

Feel free to let me know if I correctly addressed your points, ok? Still new to this whole area. :)

nice, this is a lot better! could you remove the old files? there's TestDatabaseReader.ipynb, TestPostgresReader, and postgresql.py which can be removed

jerryjliu avatar Jan 23 '23 07:01 jerryjliu

@jerryjliu, done! Let me know if I did it correctly this time. 😅

kevinqz avatar Jan 23 '23 11:01 kevinqz