postgres superuser issues during pg_upgrade - postgresql

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.

Related

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

Postgres `createuser` command not working [duplicate]

I'm trying to set up Postgres for the first time, and I need to create a user with permissions to read and create databases. However, when I use:
createuser username
in my terminal I get the following message:
createuser: could not connect to database postgres: FATAL: role "tom" does not exist
Tom is my Ubuntu user account that I'm logged into right now. I'm trying to create a username of "postgres" then do a psql -U psql template1 so I can create a database and assign an owner to it for my Rails app.
You mentioned Ubuntu so I'm going to guess you installed the PostgreSQL packages from Ubuntu through apt.
If so, the postgres PostgreSQL user account already exists and is configured to be accessible via peer authentication for unix sockets in pg_hba.conf. You get to it by running commands as the postgres unix user, eg:
sudo -u postgres createuser owning_user
sudo -u postgres createdb -O owning_user dbname
This is all in the Ubuntu PostgreSQL documentation that's the first Google hit for "Ubuntu PostgreSQL" and is covered in numerous Stack Overflow questions.
(You've made this question a lot harder to answer by omitting details like the OS and version you're on, how you installed PostgreSQL, etc.)
See git gist with instructions here
Run this:
sudo -u postgres psql
OR
psql -U postgres
in your terminal to get into postgres
NB: If you're on a Mac and both of the commands above failed jump to the section about Mac below
postgres=#
Run
CREATE USER new_username;
Note: Replace new_username with the user you want to create, in your case that will be tom.
postgres=# CREATE USER new_username;
CREATE ROLE
Since you want that user to be able to create a DB, you need to alter the role to superuser
postgres=# ALTER USER new_username SUPERUSER CREATEDB;
ALTER ROLE
To confirm, everything was successful,
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
new_username | Superuser, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
root | Superuser, Create role, Create DB | {}
postgres=#
Update/Modification (For Mac):
I recently encountered a similar error on my Mac:
psql: FATAL: role "postgres" does not exist
This was because my installation was setup with a database superuser whose role name is the same as your login (short) name.
But some linux scripts assume the superuser has the traditional role name of postgres
How did I resolve this?
If you installed with homebrew run:
/usr/local/opt/postgres/bin/createuser -s postgres
If you're using a specific version of postgres, say 10.5 then run:
/usr/local/Cellar/postgresql/10.5/bin/createuser -s postgres
OR:
/usr/local/Cellar/postgresql/10.5/bin/createuser -s new_username
OR:
/usr/local/opt/postgresql#11/bin/createuser -s postgres
If you installed with postgres.app for Mac run:
/Applications/Postgres.app/Contents/Versions/10.5/bin/createuser -s postgres
P.S: replace 10.5 with your PostgreSQL version
sudo -u postgres createuser -s tom
this should help you as this will happen if the administrator has not created a PostgreSQL user account for you. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name, in that case you need to use the -U switch.
Your error is posted in the official documentation. You can read this article.
I have copied the reason for you (and hyperlinked the URLs) from that article:
This will happen if the administrator has not created a PostgreSQL user account for you. (PostgreSQL user accounts are distinct from operating system user accounts.) If you are the administrator, see Chapter 20 for help creating accounts. You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name; in that case you need to use the -U switch or set the PGUSER environment variable to specify your PostgreSQL user name
For your purposes, you can do:
1) Create a PostgreSQL user account:
sudo -u postgres createuser tom -d -P
(the -P option to set a password; the -d option for allowing the creation of database for your username 'tom'. Note that 'tom' is your operating system username. That way, you can execute PostgreSQL commands without sudoing.)
2) Now you should be able to execute createdb and other PostgreSQL commands.
1- Login as default PostgreSQL user (postgres)
sudo -u postgres -i
2- As postgres user. Add a new database user using the createuser command
[postgres]$ createuser --interactive
3-exit
[postgres]$ exit
If you don't want to change the authentication method (ident) and mess with pg_hba.conf use this:
First login as the default user
sudo su - postgres
then access psql and create a user with the same name as the one you are login in
postgres=# CREATE USER userOS WITH PASSWORD 'garbage' CREATEDB;
you can verify your user with the corresponding roles with
postgres=# \du
Afer this you can create your database and verify it with
psql -d dbName
\l
\q
I had the same issue, i just do this
sudo su - postgres
createuser odoo -U postgres -dRSP #P for password
(odoo or user name that you want o give the postgres access)
On Windows use:
C:\PostgreSQL\pg10\bin>createuser -U postgres --pwprompt <USER>
Add --superuser or --createdb as appropriate.
See https://www.postgresql.org/docs/current/static/app-createuser.html for further options.
For Linux, you can activate SUPERUSER to enable to create databases like this:
Go to terminal/shell
Enter inside postgres using
sudo -u postgres -i
Now your terminal will be like "postgres#anish-Latitude-E7450:~$"
Enter postgres terminal using
psql
Now your terminal will be like "postgres=#"
Enter alter user [postgres_user_name] createdb;
Now the user will be having access to create db.
Exit psql using '\q'
Logout postgres using exit
Hope this helps you.
You need to first run initdb. It will create the database cluster and the initial setup
See How to configure postgresql for the first time? and http://www.postgresql.org/docs/8.4/static/app-initdb.html

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

Postgres: Permission denied for all but phpPgAdmin

Just installed OS X Server for Lion. I'd heard that the default database is now Postres - which is good news. Bad news - I can't connect to it.
I've tried using psql, createdb and Navicat, and all return the same thing: "Could not connect to server: Permission denied." This is using the _postgres role and using my own role which I've added as a superuser to postgres.
The weird thing is, phpPgAdmin has no trouble at all in connecting. Neither do I when I'm running as root and use psql -U. But if I just straight up pqsl postgres or createdb whatever then it flat-out refuses to work.
Here's a summary:
gormster$ psql postgres # permission denied
gormster$ createdb whatever # permission denied
gormster$ sudo psql postgres # role "root" does not exist
gormster$ sudo su -
root# psql postgres # role "root" does not exist
root# psql -Ugormster postgres # THIS WORKS
What is going on?
Nevermind, it magically started working again. Not sure what it is I did to get it to happen, but it may have been adding myself to the _postgres group. If you're unaware, there are instructions for adding yourself to a group here.