Postgresql - postgres#localhost failed FATAL: role does not exist - postgresql

I just installed postgres (with brew) and started it with pg_ctl -D /usr/local/var/postgres -l logfile start
I now try to connect to it in the terminal but get the following:
Connection to PostgreSQL - postgres#localhost failed
FATAL: role "me" does not exist
The issue might be that I first installed postgres.app and then didn't get it work right and then removed it.

You don't show what you typed and as which user you did it, but from the error message I'd say that you should add -U postgres to the psql invocation.

Related

How do I solve this problem to use psql? | psql: error: FATAL: role "postgres" does not exist

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"?
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
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.
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.
first check user postgres exists:
$ id postgres
Then:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.
If, Password for user postgres is no known then change it:
$ su - postgres
Password:
su: Authentication failure
$ sudo passwd postgres
New password:
Retype new password:
passwd: password updated successfully
Finally again:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.

Phoenix and Postgres install - not talking

My OS is Fedora 26
I have installed Postgresql and Phoenix.
Postgres has a superuser "postgres" with password "postgres". This is confirmed by running \du in psql.
When I run $ mix ecto.create, I get
** (Mix) The database for Hello.Repo couldn't be created: FATAL 28000 (invalid_authorization_specification): Ident authentication failed for user "postgres"
I suspect it may be a permissions issue. To log into psql requires
$ sudo -u postgres psql postgres
Whereas Phoenix when attempting to use postgres may not have sudo privilages.
$ psql --version
psql (PostgreSQL) 9.6.8
Any thoughts appreciated.
By default the authentication for the postgres database user connecting to the DB locally is to verify that the operating system user is also postgres. This is what the error message refers to as Ident authentication and is why connection after doing sudo -u postgres works.
To connect as the postgres user using another means of authentication you need to edit the pg_hba.conf file. (HBA stands for host based authentication).
The line that allows this will look like this:
local all postgres peer
Add a line that looks like this (without removing the other line!):
local all postgres md5
And you should be able to connect using the password for postgres as well.
If I remember correctly you will need to restart the DB for this to take effect.

postgres error when starting Phoenix server

I Just made a new app in phoenix. Im following along with the Programming Phoenix book. I am on chapter 3 and after making the files and using:
mix phoenix.server
I get an angry red error saying:
[error] Postgrex.Protocol (#PID<0.234.0>) failed to connect: **
(Postgrex.Error) FATAL 28000 (invalid_authorization_specification):
role "postgres" does not exist
it keeps repeating this over and over.
Edit: Changes made to reflect comments.
This will delete all local databases
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
sudo -u <your local username> psql postgres
CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;
This simply creates the Postgres user
psql -U postgres
CREATE USER postgres;
Thanks #Dogbert, #mudasobwa

role "postgres" does not exist; cannot createuser

I am on Linux Mint Cinnamon 2.2.16.
During the process of getting Rails up and running, I am having problems with Postgres.
postgres#BL ~ $ psql --version
psql (Postgres-XC) 1.1
(based on PostgreSQL) 9.2.4
I was unable to get anything working under my usual username, so I changed to the default user using
sudo su - postgres
I cannot get anything to work with createuser.
postgres#BL ~ $ psql
psql: FATAL: role "postgres" does not exist
postgres#BL ~ $ createuser -s -U $USER
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
postgres#BL ~ $ sudo -u postgres createuser newname
Sorry, user postgres is not allowed to execute '/usr/bin/createuser newname' as postgres on BL.
postgres#BL ~ $ which psql
/usr/bin/psql
postgres#BL ~ $ psql \l
psql: FATAL: role "postgres" does not exist
After thoroughly researching the problem and tearing out more than a few hairs, I decided this was some variation of a problem with packaging/installation, similar problem noted here: unable to create user postgres: role "postgres" does not exists
I did a complete uninstall, as per below, and reinstalled without postgresql-xc
How to thoroughly purge and reinstall postgresql on ubuntu?
The new install had expected behavior with the user "postgres" and I was able to add myself as a superuser and create new databases. After some post-installation finagling, Rails seems to be running and playing nice with postgres.
You have to authenticate to psql as the superuser in order to manage users.
For example
psql -U root
Afterwards
create user paige with password 'paige';

How to install progresql-9.3 properly

I followed the installation on this document http://www.postgresql.org/download/linux/ubuntu/ and every thing seems to be fine. But when I try to this follow this tutorial: http://www.postgresql.org/docs/9.3/static/tutorial-createdb.html, everything is not fine any more. This is the error I got when I try to create a database table:
$ createdb mydb
WARNING: password file "/home/.../.pgpass" has group or world access; permissions should be u=rw (0600) or less
WARNING: password file "/home/.../.pgpass" has group or world access; permissions should be u=rw (0600) or less
createdb: could not connect to database template1: FATAL: role "..." does not exist
$ /usr/local/pgsql/bin/createdb mydb
bash: /usr/local/pgsql/bin/createdb: No such file or directory
You got one warning and one error.
You can handle the warning by this command of line in your terminal:
$ chmod 600 ~/.pgpass
When you write "psql" in your terminal, Postgres DBMS try to connect to the one database with your computer name but it can't find it. In other hand Postgrest create a database named "postgres" when it install so try connect to this and create your database. You can connect to "postgres" database easily with this command:
$ psql postgres
If you get the "connections on Unix domain socket "/tmp/.s.PGSQL.5432"?" error start your database with this command:
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Check the result with this command:
$ pg_ctl -D /usr/local/var/postgres status
The error message tells you what's wrong. Change the permissions on .pgpass:
chmod u=rw $HOME/.pgpass
Also, unless you edited the error message and didn't say so, you appear to actually be attempting to connect as user .... I imagine the examples you read said something like:
psql -U "..." template1
and assumed you'd replace "..." with the actual username.