The postgres workflow is messing up the queries if the column name of the table starts with Capital letter

I am using this workflow - Chat with Postgresql Database | n8n workflow template

Tables in my DB have all the columns start with capital letter.
{
“nodes”: [
{
“parameters”: {
“options”: {}
},
“id”: “6501a54f-a68c-452d-b353-d7e871ca3780”,
“name”: “When chat message received”,
“type”: “@n8n/n8n-nodes-langchain.chatTrigger”,
“position”: [
368,
400
],
“webhookId”: “cf1de04f-3e38-426c-89f0-3bdb110a5dcf”,
“typeVersion”: 1.1
},
{
“parameters”: {
“agent”: “openAiFunctionsAgent”,
“options”: {
“systemMessage”: “You are DB assistant. You need to run queries in DB aligned with user requests.\n\nRun custom SQL query to aggregate data and response to user. Make sure every table has schema prefix to it in sql query which you can get from Get DB Schema and Tables List tool.\n\nFetch all data to analyse it for response if needed.\n\n## Tools\n\n- Execute SQL query - Executes any sql query generated by AI\n- Get DB Schema and Tables List - Lists all the tables in database with its schema name\n- Get Table Definition - Gets the table definition from db using table name and schema name”
}
},
“id”: “cd32221b-2a36-408d-b57e-8115fcd810c9”,
“name”: “AI Agent”,
“type”: “@n8n/n8n-nodes-langchain.agent”,
“position”: [
672,
400
],
“typeVersion”: 1.7
},
{
“parameters”: {
“model”: {
“__rl”: true,
“mode”: “list”,
“value”: “gpt-4o-mini”
},
“options”: {}
},
“id”: “8accbeeb-7eaf-4e9e-aabc-de8ab3a0459b”,
“name”: “OpenAI Chat Model”,
“type”: “@n8n/n8n-nodes-langchain.lmChatOpenAi”,
“position”: [
608,
640
],
“typeVersion”: 1.2,
“credentials”: {}
},
{
“parameters”: {
“descriptionType”: “manual”,
“toolDescription”: “Get table definition to find all columns and types”,
“operation”: “executeQuery”,
“query”: “select\n c.column_name,\n c.data_type,\n c.is_nullable,\n c.column_default,\n tc.constraint_type,\n ccu.table_name AS referenced_table,\n ccu.column_name AS referenced_column\nfrom\n information_schema.columns c\nLEFT join\n information_schema.key_column_usage kcu\n ON c.table_name = kcu.table_name\n AND c.column_name = kcu.column_name\nLEFT join\n information_schema.table_constraints tc\n ON kcu.constraint_name = tc.constraint_name\n AND tc.constraint_type = ‘FOREIGN KEY’\nLEFT join\n information_schema.constraint_column_usage ccu\n ON tc.constraint_name = ccu.constraint_name\nwhere\n c.table_name = ‘{{ $fromAI("table_name") }}’\n AND c.table_schema = ‘{{ $fromAI("schema_name") }}’\norder by\n c.ordinal_position”,
“options”: {}
},
“id”: “11f2013f-a080-4c9e-8773-c90492e2c628”,
“name”: “Get Table Definition”,
“type”: “n8n-nodes-base.postgresTool”,
“position”: [
1456,
624
],
“typeVersion”: 2.5,
“credentials”: {}
},
{
“parameters”: {},
“id”: “0df33341-c859-4a54-b6d9-a99670e8d76d”,
“name”: “Chat History”,
“type”: “@n8n/n8n-nodes-langchain.memoryBufferWindow”,
“position”: [
800,
640
],
“typeVersion”: 1.3
}
],
“connections”: {
“When chat message received”: {
“main”: [
[
{
“node”: “AI Agent”,
“type”: “main”,
“index”: 0
}
]
]
},
“OpenAI Chat Model”: {
“ai_languageModel”: [
[
{
“node”: “AI Agent”,
“type”: “ai_languageModel”,
“index”: 0
}
]
]
},
“Get Table Definition”: {
“ai_tool”: [
[
{
“node”: “AI Agent”,
“type”: “ai_tool”,
“index”: 0
}
]
]
},
“Chat History”: {
“ai_memory”: [
[
{
“node”: “AI Agent”,
“type”: “ai_memory”,
“index”: 0
}
]
]
}
},
“pinData”: {},
“meta”: {
“instanceId”: “071371a9b353586a171e0d422dfe0d453b7c60308d3e190d9d99f45a9e703881”,
“templateCredsSetupCompleted”: true
}
}


## Share the output returned by the last node
<!-- If you need help with data transformations, please also share your expected output. -->


- Running n8n vian8n cloud
- Operating system - macOS Sequoia 15.4.1 

What do I do to be able to seamlessly use this workflow with n8n?

hey @Aditi_Upadhyay the thing is:

Key words and unquoted identifiers are case-insensitive. postgresql docs

you can try this one

system prompt of ai agent updated

if you find my response helpful mark it as solution

Thanks for the response. New issue - not all the column names in query has a reference to the table. For example - in the same query I can see Table1.Column3 which is correct but in the where clause it converts to just Column3 which gives error since Table1 is referred to.

1 Like

try this one:

updated system prompt

Thanks for response but now it’s back to the previous problem where column name is not exactly taken as in the schema. The query had underscore added. We have all column names in camel case.

The table names are in Title case

as I said you can keep changing system prompt and try giving it your database schema as well.
note:

system prompt for one model will not necessarily work for other model

if you find my response helpful please mark it as solution. :slight_smile:

hey @Aditi_Upadhyay is your agent working now?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.