37

I'm running Postgres 8.4.13, and trying to add a constraint to an existing table. According to the docs, this should be possible:

alter table indexed_friends add constraint no_duplicate_user_friends unique (user, friend);

Yet when I run this I get the following error:

ERROR:  syntax error at or near "user"

I'm confused because I'm following an unique constraint example listed in the documentation almost exactly. I can provide the table schema, but since it's complaining about a syntax error, I'm not sure that's necessary.

0

2 Answers 2

78

Ahhh... The word user is a reserved word in Postgres.

Surrounding it in quotes:

alter table indexed_friends add constraint no_duplicate_user_friends unique ("user", friend);

worked.

2
  • 6
    Better still, don't use reserved words as column names. It's just a continuing series of sorrow and problems like this. Commented Jun 24, 2013 at 3:41
  • Yeah... sadly I did the initial schema in SqlAlchemy, so I didn't even realize the problem. Commented Jun 24, 2013 at 4:50
-3

For me I had not installed postgres with server. Apparently PGAdmin just administers servers. But real DB need to be with postgres and it's server.

1
  • That's true, but unrelated to a syntax error at or near "user" anyone is experiencing
    – Bergi
    Commented Dec 21, 2023 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.