| --- |
| title: '🐘 Postgres' |
| --- |
|
|
| 1. Setup the Postgres loader by configuring the postgres db. |
| ```Python |
| from embedchain.loaders.postgres import PostgresLoader |
|
|
| config = { |
| "host": "host_address", |
| "port": "port_number", |
| "dbname": "database_name", |
| "user": "username", |
| "password": "password", |
| } |
|
|
| """ |
| config = { |
| "url": "your_postgres_url" |
| } |
| """ |
|
|
| postgres_loader = PostgresLoader(config=config) |
|
|
| ``` |
|
|
| You can either setup the loader by passing the postgresql url or by providing the config data. |
| For more details on how to setup with valid url and config, check postgres [documentation](https://www.postgresql.org/docs/current/libpq-connect.html |
|
|
| NOTE: if you provide the `url` field in config, all other fields will be ignored. |
|
|
| 2. Once you setup the loader, you can create an app and load data using the above postgres loader |
| ```Python |
| import os |
| from embedchain.pipeline import Pipeline as App |
|
|
| os.environ["OPENAI_API_KEY"] = "sk-xxx" |
|
|
| app = App() |
|
|
| question = "What is Elon Musk's networth?" |
| response = app.query(question) |
| |
|
|
| app.add("SELECT * FROM table_name;", data_type='postgres', loader=postgres_loader) |
| |
|
|
| response = app.query(question) |
| |
| ``` |
|
|
| NOTE: The `add` function of the app will accept any executable query to load data. DO NOT pass the `CREATE`, `INSERT` queries in `add` function as they will result in not adding any data, so it is pointless. |
|
|
| 3. We automatically create a chunker to chunk your postgres data, however if you wish to provide your own chunker class. Here is how you can do that: |
| ```Python |
|
|
| from embedchain.chunkers.postgres import PostgresChunker |
| from embedchain.config.add_config import ChunkerConfig |
|
|
| postgres_chunker_config = ChunkerConfig(chunk_size=1000, chunk_overlap=0, length_function=len) |
| postgres_chunker = PostgresChunker(config=postgres_chunker_config) |
|
|
| app.add("SELECT * FROM table_name;", data_type='postgres', loader=postgres_loader, chunker=postgres_chunker) |
| ``` |