Search code examples
postgresqlpgadmin

Postgresql: Querying Varchar Requires Curly Braces?


EDIT: This was an issue of pgAdmin4 displaying I could change my previously declared column data type of character varying [] to character varying (when the user could never do that), leading me to believe I was querying for a varchar, when in reality I was querying a varchar[].

Why does querying a varchar column require curly braces like so?

SELECT * FROM users WHERE email = '{[email protected]}'

Removing the curly braces prompts ERROR: malformed array literal...

I'm using (PostgreSQL) 10.4 and pgAdmin 4 as an interface. The email column is of type character varying [], pgAdmin4 won't let me change it to character varying.


Solution

  • The reason is simple - this is not a varchar column.

    Notice square brackets? character varying []
    That means it's array of strings.

    You can read more about arrays here: https://www.postgresql.org/docs/current/static/arrays.html

    By using curly brackets you create an array of one element: '{[email protected]}'

    And now you're comparing apples to apples (array to array)

    pgAdmin doesn't allow you to convert the column because it doesn't "know" how exactly you want to convert array of strings to a single string.

    You can check which functions are available on arrays in PostgreSQL here: https://www.postgresql.org/docs/current/static/functions-array.html