PostgreSQL error: OperationalError: FATAL: role “username” does not exist

postgresql

I got this error When I'm trying to run the Code.

Error

I searched the site like this problem

But It couldn't solve my problem.

When I'm trying to create superuser with my username (which is TCOYUKSEL) , It create another one with "tcoyuksel"

Error

ozgur.py Line 9 db = psycopg2.connect("dbname=news")

ozgur.py Line 73 get_pop_articles(), str("views"))

init.py Line 130 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
–I didn't do anything with this file–

Best Answer

The role name is case sensitive, but with your queries it gets translated to lower case tcoyuksel,

CREATE ROLE TCOYUKSEL superuser;
ALTER ROLE TCOYUKSEL WITH LOGIN;

as key words and unquoted identifiers are case insensitive (4.1.1. Identifiers and Key Words).

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named “select”, whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.

You need to use the quotes to bypass the normalization i.e. use TCOYUKSEL as delimited identifier:

CREATE ROLE "TCOYUKSEL" superuser;
ALTER ROLE "TCOYUKSEL" WITH LOGIN;

If you run it directly from the command line, you need to double the quotes:

postgres "CREATE ROLE ""TCOYUKSEL"" superuser; ALTER ROLE ""TCOYUKSEL"" WITH LOGIN;"