2
votes

I'm having trouble using PostgreSQL. I have recently installed this version (13+223.pgdg20.04+1) of postgresql package in ubuntu 20.04.

I'm trying to run psql command, but I get the following error:

psql: error: FATAL: role "my_username" does not exist

I have tried to create a new user with createuser me, but I get the following error:

createuser: error: could not connect to database template1: FATAL: role "my_username" does not exist

I have tried also forcing the postgres user with createuser me --username=postgres, but I get the following error:

createuser: error: could not connect to database template1: FATAL: Peer authentication failed for user "postgres"

How do I solve these problems to use PostgreSQL locally on my computer without these problems?

PD: I have reinstalled postgres and now I'm getting a different error while doing psql:

psql: error: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

3

3 Answers

1
votes

Peer authentication means (there are advanced possibilities, but those are not going to be used by default, while the simple method is the default for apt-installed PostgreSQL) that you have to be the OS user 'postgres' to connect as the database user 'postgres'. So you would do:

sudo -u postgres createuser me

You don't need to specify --username=postgres, since that is the default behavior anyway once you use sudo -u postgres

Alternatively, you could change your pg_hba.conf to use a different authentication method other than peer, if you want to.

1
votes

I'm not sure why I had a bad installation, but I have completely uninstalled postgres following this post:

https://kb.objectrocket.com/postgresql/how-to-completely-uninstall-postgresql-757

after that I have restarted my computer and installed posgres again following the proper instructions in:

https://www.postgresql.org/download/linux/ubuntu/

and now it looks like it works without problems

0
votes

You need to provide username in the psql command using -U option.

psql -U postgres

Postgresql comes with a predefined superuser role called postgres. If you want to create more roles, you first have to connect as this initial role.