Search code examples
postgresqlquoted-identifier

creating a table with column names having brackets


I tried to create a table with postgresql query:

CREATE TABLE customer_account(ID_account integer primary key, customer_name (lastname) text);

but it gives an error message:

ERROR:  syntax error at or near "("
LINE 5: customer_name (lastname) text,

Probably the problem comes from the bracket, and I already tried like

CREATE TABLE customer_account("ID_account" primary key, "customer_name (lastname)" text);

But it also gave me a similar error message.

How to correct the query? I really need to use bracket.


Solution

  • Using " will work, but you are missing the data type for your primary key:

    CREATE TABLE customer_account
    (
      "ID_account" integer primary key, 
                 --^ here
      "customer_name (lastname)" text
    );

    Online example


    But I strongly suggest you do not use quoted identifiers.

    They will give you much more trouble in the long run then they are worth it.

    I would recommend to use something like this:

    CREATE TABLE customer_account
    (
      account_id        integer primary key, 
      customer_lastname text
    );
    

    ("ID" as a prefix sounds quite strange in English)