role "postgres" does not exist; cannot createuser - postgresql

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';

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.

postgres superuser issues during pg_upgrade

I'm using homebrew on Mac OSX to manage a postgres database. I'm trying to upgrade my postgres install from 11.9 to 13.0 using pg_upgrade. When I run pg_upgrade I get the following error about not being a superuser. If I try running as the "postgres" user, I get an error that the "postgres" user is not the install user.
pg_upgrade --old-datadir /usr/local/var/postgresql#11 --new-datadir /usr/local/var/postgres --old-bindir /usr/local/Cellar/postgresql#11/11.9/bin --new-bindir /usr/local/Cellar/postgresql/13.0/bin -c -U Brian
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
connection to database failed: FATAL: must be superuser to connect in binary upgrade mode
could not connect to source postmaster started with the command:
"/usr/local/Cellar/postgresql#11/11.9/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/usr/local/var/postgresql#11" -o "-p 50432 -b -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/usr/local/Cellar'" start
Failure, exiting
Try as the "postgres" user
pg_upgrade --old-datadir /usr/local/var/postgresql#11 --new-datadir /usr/local/var/postgres --old-bindir /usr/local/Cellar/postgresql#11/11.9/bin --new-bindir /usr/local/Cellar/postgresql/13.0/bin -c -U postgres
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
Checking database user is the install user
database user "postgres" is not the install user
My system username is "Brian" and the original 11.9 database was installed as that user, but somehow it does not have superuser rights. I'm not sure how that happened but never quite realized it until now. My 13.0 database seems to be correctly set up with "Brian" as a superuser.
template1=# \du+
List of roles
Role name | Attributes | Member of | Description
---------------+-------------------------------------+------------+-------------
Brian | Create role, Create DB, Replication | {} |
When I login with psql -d template1 -U postgres and try to alter the role I get the following error.
template1=# alter role Brian with superuser;
ERROR: role "brian" does not exist
Time: 0.415 ms
If I log in as sudo -u postgres -i and try to alter or create a user, I also get the following errors:
psql -c "alter role Brian with superuser;"
ERROR: role "brian" does not exist
createuser -s Brian
createuser: error: creation of new role failed: ERROR: role "Brian" already exists
Does anyone know why postgres is confused over "Brian" and "brian" and how I can give superuser rights the role "Brian" so I can properly perform a pg_upgrade? When I tried brew postgresql-upgrade-database it initially gave me the same superuser error and now, upon rerun, it says that everything is already upgraded.
Case of SQL identifiers from within SQL is ignored except when in double quotes:
alter role "Brian" with superuser;
But when specified on the command line (with -U, for example) case is not ignored.

Cannot access postgres PSQL

I have just installed postgres (mac os), however, when I go to access psql it tries to find the database not the user.
I know the username is benbagley (because that's my system name)
I have tried.
➜ ~ psql
psql: FATAL: database "benbagley" does not exist
and
➜ ~ psql -U benbagley
psql: FATAL: database "benbagley" does not exist
As stated in the manual psql assumes that database name is same as the username when the database is not provided explicitly and tries to connect with that database, which does not exist in your case.
Try connecting with default database which is postgres.
psql -U username -d postgres

Create local Postgresql account

On OS X 10.11.2, I've installed Postgres.app and I'm running the local server. I'm trying to create a local account with a username and password so that I can develop a Rails app locally. However, running the following command:
sudo -u postgres createuser -s {USERNAME}
I receive sudo: unknown user: postgres error.
Any suggestions as to why this error occurs and how to resolve this?
sudo tells you there is no system user "postgres".
When you installed PostgreSQL, it should have created database user "postgres" and you can try use that:
$ psql -u postgres
postgres=# create user {username} password '{password}';
I managed to create a user with the following steps.
In the terminal:
createuser --superuser {USERNAME}
I then set a password in psql:
\password {USERNAME}

Postgresql initial configuration: How to access as the postgres user?

After installing postgresql, I tried it out, typing createdb mydb, like it's written in the documentation. Then the following error occured:
createdb: could not connect to database postgres: FATAL: role "xxx" does not exist
I studied the documentation, where is said:
You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account
I tried this by accessing psql (in my case with sudo -u postgres psql, using Ubuntu 12.10).
But then what should I do?
if the db is owned by user postgres you can do the following
createdb -U postgres dbname
since by default postgresql will trust connections from localhost.
su - postgres
and after you have been logged in:
createdb mydb