# PostgresChatMessageHistory

> **Class** in `langchain_postgres`

📖 [View in docs](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory)

## Signature

```python
PostgresChatMessageHistory(
    self,
    table_name: str,
    session_id: str,
    /,
    *,
    sync_connection: Optional[psycopg.Connection] = None,
    async_connection: Optional[psycopg.AsyncConnection] = None,
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `session_id` | `str` | Yes | The session ID to use for the chat message history |
| `table_name` | `str` | Yes | The name of the database table to use |
| `sync_connection` | `Optional[psycopg.Connection]` | No | An existing psycopg connection instance (default: `None`) |
| `async_connection` | `Optional[psycopg.AsyncConnection]` | No | An existing psycopg async connection instance (default: `None`) |

## Extends

- `BaseChatMessageHistory`

## Constructors

```python
__init__(
    self,
    table_name: str,
    session_id: str,
    /,
    *,
    sync_connection: Optional[psycopg.Connection] = None,
    async_connection: Optional[psycopg.AsyncConnection] = None,
) -> None
```

| Name | Type |
|------|------|
| `table_name` | `str` |
| `session_id` | `str` |
| `sync_connection` | `Optional[psycopg.Connection]` |
| `async_connection` | `Optional[psycopg.AsyncConnection]` |


## Properties

- `messages`

## Methods

- [`create_tables()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/create_tables)
- [`acreate_tables()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/acreate_tables)
- [`drop_table()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/drop_table)
- [`adrop_table()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/adrop_table)
- [`add_messages()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/add_messages)
- [`aadd_messages()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/aadd_messages)
- [`get_messages()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/get_messages)
- [`aget_messages()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/aget_messages)
- [`clear()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/clear)
- [`aclear()`](https://reference.langchain.com/python/langchain-postgres/chat_message_histories/PostgresChatMessageHistory/aclear)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-postgres/blob/9d819182da7e3fb1b815e6c9a6b7c2cd1eb4bc0e/langchain_postgres/chat_message_histories.py#L78)