11

When trying to connect to my postgres server in Azure from psql client, I get the following error, even though I am using the correct username. How can I fix this?

psql: FATAL: Invalid Username specified. Please check the Username and retry connection. The Username should be in <username@hostname> format.

Shantanu
  • 2,871
  • 4
  • 24
  • 38

3 Answers3

15

As noted in the error text, you are required to follow the <username@hostname> format when trying to connect to postgresql server, whether you are doing it from psql client or using pgadmin. Using <username@hostname> format instead of just <username> should get rid of the error.

Read the quick-start documents for Azure portal and CLI to understand more about how to create and configure your postgres server.

Shantanu
  • 2,871
  • 4
  • 24
  • 38
  • 5
    The @ sign in username works fine for objects but not connect strings. According to URI RFC-3986 username allows hex encoding. So replace the @ with %40. user@host:pw@fullhost becomes user%40host:pw@fullhost – Kevin Burandt Apr 25 '18 at 01:37
  • @KevinBurandt, you should add your comment as an answer, – rjdkolb Mar 16 '20 at 12:01
  • As of May 5th 2023, this is still the case - `postgresql://username@hostname:pw@hostname:port/dbname`. The hostname will appear twice when using this as a connection string to sqlalchemy's `create_engine` – Snow24 May 05 '23 at 02:45
11

The @ sign in username works fine for objects but not connect strings. According to URI RFC-3986 username allows hex encoding. So replace the @ with %40. user@host:pw@fullhost becomes user%40host:pw@fullhost

Kevin Burandt
  • 1,970
  • 1
  • 14
  • 10
0

In general, I have concluded that while trying to connect into PostgreSQL, you need to follow this username pattern: username@hostname. For newer versions of postgresql (after v11) this is not prerequisite and you can use only as per documents. But the word prerequisite, I think is wrong, as for every PostgreSQL v15 the username must be . Finally if the host is localhost, the username should be even for older versions.

Stavros Koureas
  • 1,126
  • 12
  • 34