langchain icon indicating copy to clipboard operation
langchain copied to clipboard

AttributeError: 'PostgresChatMessageHistory' object has no attribute 'cursor'

Open ravi160822 opened this issue 1 year ago • 9 comments

getting this error when using postgreschatmessagehistory

ravi160822 avatar Apr 21 '23 10:04 ravi160822

@ravi160822 can you please share a minimal code to reproduce the bug ?

AmanSal1 avatar Apr 21 '23 14:04 AmanSal1

just ran this, https://python.langchain.com/en/latest/modules/memory/examples/postgres_chat_message_history.html

ravi160822 avatar Apr 22 '23 20:04 ravi160822

same issue

racinger avatar Apr 22 '23 21:04 racinger

@ravi160822 @racinger are you connected to a running database? If the connection_string is invalid then psycopg.connect will error and self.cursor will never be set. Please ensure you pass a valid connection_string for a running postgres db and try again.

joelsimonoff avatar Apr 24 '23 23:04 joelsimonoff

facing the same issue. @joelsimonoff please elaborate running database. I have the database up with an empty table though, I am getting the same AttributeError 'PostgresChatMessageHistory' object has no attribute 'cursor'

lordskyzw avatar May 03 '23 19:05 lordskyzw

I ran into the same issue. It was a dependency issue for me. I had to install the following; sudo apt-get install libpq-dev
pip install psycopg

Note I already have psycopg2 installed b/c I'm using postgres in my app.

robynph avatar May 17 '23 16:05 robynph

Tried:

sudo apt-get install libpq-dev
pip install psycopg

But it still shows this error:

File python3.8/site-packages/langchain/memory/chat_message_histories/postgres.py", line 83, in __del__
    if self.cursor:
AttributeError: 'PostgresChatMessageHistory' object has no attribute 'cursor'

maejain-deloitte avatar May 21 '23 05:05 maejain-deloitte

I simply changed to using MongoDB

lordskyzw avatar May 21 '23 05:05 lordskyzw

I fixed it as well:

Steps I took:

sudo apt-get install libpq-dev
pip install psycopg
pip install psycopg-binary

Connection string needs to be correct:

history = PostgresChatMessageHistory(connection_string="postgresql://<connection_name>:<password>@localhost:<port>/<db_name>", session_id="foo")

For me this looked like:

history = PostgresChatMessageHistory(connection_string="postgresql://postgres:postgres@localhost:5432/postgres", session_id="foo")

Just recheck your file with this: https://python.langchain.com/en/latest/_modules/langchain/memory/chat_message_histories/postgres.html#PostgresChatMessageHistory

It works now!

maejain-deloitte avatar May 21 '23 05:05 maejain-deloitte

I'm also unable to connect to my db. I think it might have something to do with this line:

self.connection = psycopg.connect(connection_string)

This seems to hang and then I end up with no connection

For context, I've deployed my pg server with docker and am trying to run the doc sample notebook locally. I'm not sure if this is possible as it seems that they need to be launched together from a docker file (new to Docker, so likely operator error as well here)

djpecot avatar Jun 06 '23 01:06 djpecot

Hi For those still getting the error as connection is bad: Name or service not known or AttributeError: 'PostgresChatMessageHistory' object has no attribute 'cursor' and you have tried @maejain-deloitte steps and still get error? Just make sure you don't have any special characters in your password. Somehow having @ or / or . in your password will cause the error. Hope helps if someone face the same issue in future. Btw thank you @maejain-deloitte

fadilparves avatar Aug 21 '23 08:08 fadilparves

getting error:..AttributeError: 'PostgresChatMessageHistory' object has no attribute 'cursor'

Aman78695 avatar Aug 25 '23 09:08 Aman78695

@Aman78695 did you install pyscopg lib? Not the pyscopg2 library and make sure there is no special chars in your password because I realize that when passing password with special chars into the connection string it seems to cause the error saying port is password.

fadilparves avatar Aug 25 '23 09:08 fadilparves

Hi, @ravi160822! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you reported an issue regarding the 'PostgresChatMessageHistory' object not having the 'cursor' attribute, resulting in an AttributeError. AmanSal1 requested a minimal code to reproduce the bug, and you provided a link to a code example. joelsimonoff suggested that the issue might be due to an invalid connection_string and advised ensuring a valid connection to a running Postgres database. Some users reported that installing the libpq-dev and psycopg packages resolved the issue, while others switched to using MongoDB. maejain-deloitte provided detailed steps to fix the issue, including installing the necessary packages and ensuring the correct connection string.

Based on the resolution, it seems that the issue has been resolved by following the steps provided by maejain-deloitte, which include installing the necessary packages and ensuring the correct connection string.

Before we close this issue, we wanted to check with you if the issue is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository! Let us know if you have any further questions or concerns.

dosubot[bot] avatar Nov 24 '23 16:11 dosubot[bot]