syntax error at or near "-" in PostgreSQL
Asked Answered
S

5

43

I'm trying to run a query to update the user password using.

alter user dell-sys with password 'Pass@133';

But because of - it's giving me error like,

ERROR:  syntax error at or near "-"
LINE 1: alter user dell-sys with password 'Pass@133';
                       ^

Can Anyone shade a light on it?

Sergiosergipe answered 6/9, 2013 at 11:18 Comment(1)
postgresql.org/docs/current/static/…Brassie
A
61

I have reproduced the issue in my system,

postgres=# alter user my-sys with password 'pass11';
ERROR:  syntax error at or near "-"
LINE 1: alter user my-sys with password 'pass11';
                       ^

Here is the issue,

psql is asking for input and you have given again the alter query see postgres-#That's why it's giving error at alter

postgres-# alter user "my-sys" with password 'pass11';
ERROR:  syntax error at or near "alter"
LINE 2: alter user "my-sys" with password 'pass11';
        ^

Solution is as simple as the error,

postgres=# alter user "my-sys" with password 'pass11';
ALTER ROLE
Animatism answered 6/9, 2013 at 12:24 Comment(0)
T
28

Wrap it in double quotes

alter user "dell-sys" with password 'Pass@133';

Notice that you will have to use the same case you used when you created the user using double quotes. Say you created "Dell-Sys" then you will have to issue exact the same whenever you refer to that user.

I think the best you do is to drop that user and recreate without illegal identifier characters and without double quotes so you can later refer to it in any case you want.

Terrellterrena answered 6/9, 2013 at 11:19 Comment(2)
Tried this one, it's giving ERROR: syntax error at or near "alter" LINE 2: alter user "dell-sys" with password 'Pass@133';Sergiosergipe
@Heliconia are you sure your previous line is ended with semicolon?Ganesha
H
1

i was trying trying to GRANT read-only privileges to a particular table to a user called walters-ro. So when i ran the sql command # GRANT SELECT ON table_name TO walters-ro; --- i got the following error..`syntax error at or near “-”

The solution to this was basically putting the user_name into double quotes since there is a dash(-) between the name.

# GRANT SELECT ON table_name TO "walters-ro";

That solved the problem.

Hymettus answered 27/4, 2020 at 17:32 Comment(0)
I
0

I got the similar error below:

ERROR: syntax error at or near ")"
LINE 4: );

Because I put a trailing comma mistakenly as shown below:

CREATE TABLE person(
  id SERIAL PRIMARY KEY,
  name VARCHAR(20),
               -- ↑ A trailing comma
);

So, I removed the a trailing comma as shown below, then the error was solved:

CREATE TABLE person(
  id SERIAL PRIMARY KEY,
  name VARCHAR(20)
               -- ↑ No trailing comma
);
Impinge answered 22/1, 2023 at 13:33 Comment(0)
W
0

In case you're in 2024 and facing this issue while resetting the password All you need to do is just wrap your password in single quotes it'll work

alter user postgres with password 'Pass@133';

Whip answered 19/4 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.