PostgreSQL Lookup returns no results – despite correct chat_id match

Hey everyone,

I’m completely stuck with a PostgreSQL Lookup node in n8n. I’m trying to match a chat_id against a value stored in my database. The chat_id column is of type TEXT, and the value I’m passing into the node is 100% correct.

I’m using this as the query:

SELECT * FROM public.conversations WHERE chat_id = $1::text;

And I’m passing the value using this expression:
{{ $node["ChatID Extraktion"].json["chat_id"].toString().trim() }}

Despite this, the query returns no results, even though the ID is clearly in the database.

What I’ve already checked:

  • The chat_id exists in the DB and matches exactly
  • No extra whitespace or formatting issues
  • If I hardcode the value directly into the query, it works
  • I also tried converting everything to hex – turns out the incoming value is fine (3637...) but at some point n8n seems to pass the expression as a literal string, not as an evaluated value

It’s like the parameter isn’t being resolved correctly. I even tried various casting approaches, LIKE queries, etc. Still no results.

I’m losing my mind at this point. Has anyone had this issue where the value just doesn’t resolve or match in a query, even though all formatting and types seem fine?

Would seriously appreciate any ideas. Thanks in advance.

Please share your workflow




## Information on your n8n setup
- **n8n version:** = 1.91.3
- **Database (default: SQLite):** runs on pgAdmin
- **Running n8n via self Hostet cloud:**
- **Operating system:** pgAdmin --> Postgres

Hey @MN_Agency ,

Try using this query

SELECT * FROM public. conversations WHERE chat_id =  '{{ $node["ChatID Extraktion"].json["chat_id"].toString().trim() }}'

remove the query parameter.

Hey @Sudhanshu_Sharma,

thanks for your help! still doesn’t work. :frowning:

Hey @MN_Agency,

I noticed there’s an extra space in public.conversations

I believe I may have accidentally added it yesterday while sharing the solution with you.

SELECT * FROM public.conversations WHERE chat_id = '{{ $node["ChatID Extraktion"].json["chat_id"].toString().trim() }}'

Please remove the space between public.conversations and try again. :blush:

Hello @Sudhanshu_Sharma,

Still doesn’t work. I even deleted the database and set it up from scratch. The ‘ChatID extraction’ now takes exactly the value that comes from the node’s output. Before, I was using {{ $('Telegram Trigger').item.json.message.chat.id }} directly. I thought that might be the issue, but unfortunately the Chat ID still isn’t being passed correctly. :frowning: Still, thank you so much for trying to help me :slight_smile:

Ohh okay @MN_Agency , if you pass the chat id directly, I mean statically instead of expression…is it working?

Yes, @Sudhanshu_Sharma when I enter the chat ID manually, it’s just not recognized as an expression. You can see it in the screenshot — once with the chat ID entered manually, and once using the expression directly from the ChatID extraction.


There are 2 items because I uploaded it twice during my tests.

@MN_Agency , can you please share what exact value you are getting in this node from json.chat_id?

This is weird, we are missing something here…

Want to hop on a quick call?

Please DM me here only, I’ll share the link…

Let’s solve this problem today…it’s already 2 days since you are facing this problem

1 Like

@Sudhanshu_Sharma, yes, I’d be happy to jump on a call! Unfortunately, I can’t send you a message here yet since I’m still new to the platform. :frowning:

@Sudhanshu_Sharma could you please send me a DM im going to freak out about that :frowning:

Hey @MN_Agency just dmed you…

1 Like

Finally, after 5 days, we solved this problem!

It was a pretty weird issue involving the PostgREST node.

The issue:

  • He was trying to get a record from a table using an SQL query. When he passed the chat_id directly, everything worked fine. But when using an expression, it wasn’t returning the expected output.
  • We tried 2–3 different methods to solve this…it was honestly a bit crazy.
  • But finally, we found a solution that worked for him!

:white_check_mark: The Solution:

  • Instead of using the “Execute SQL” operation to retrieve the row, we used the default n8n operation “Select a row from the table”, and it worked!

Sometimes the simplest solutions are the hardest to find. I’m really happy that he can finally move forward!

@MN_Agency , please share the workflow here for future reference.

2 Likes

THANK YOU VERY MUCH!
we solved this problem!

here is the workflow after we updated the Node.

and the Node itself:

Now I can finally continue with the actual workflow — thanks again to @Sudhanshu_Sharma

1 Like

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