Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert text to bytea in Postgres? #39

Open
japananh opened this issue Jan 21, 2025 · 0 comments
Open

How to convert text to bytea in Postgres? #39

japananh opened this issue Jan 21, 2025 · 0 comments
Assignees

Comments

@japananh
Copy link
Owner

This query will return an error: [22P02] ERROR: invalid input syntax for type bytea.

select sha256((theme_page_id::text || component)::bytea) from page_sections where shop_id = 7287358554485297152;

The error occurs because the direct casting of a TEXT value to BYTEA using ::BYTEA can fail if the input TEXT contains characters that aren't valid for a BYTEA representation. PostgreSQL expects BYTEA input to be encoded in a specific format, such as hexadecimal or escaped strings.

Solution: using CONVERT_TO function that take a TEXT value and an encoding, then returns a BYTEA value.

select convert_to(theme_page_id::text || component, 'UTF8') from page_sections where shop_id = 7287358554485297152;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant