Accidently removed postgres default superuser privileges - can I get it back using pgAdmin? - postgresql

From inside Webmin I accidently unmarked the checkbox "Can create databases?" and "Can create users?"
Stupid, I know.
But since it takes a user with superuser privileges to edit/create a user, is there a way to fix this using pgAdmin?

If you just set NOCREATEROLE and NOCREATEDB on the user postgres, you can simply undo that.
If you actually removed the superuser privilege, you will have to start in single user mode (you cannot use pgAdmin for that):
First, stop the database.
Then, start it in single user mode:
/path/to/postgres --single -D /path/to/data/directory postgres
Then enter
ALTER ROLE postgres SUPERUSER
No trailing semicolon!
Type Ctrl+D to exit (or whatever sends EOF on your system).
Start PostgreSQL in the normal way again.

Related

postgres login asks for non superuser password

I set up postgres according to the instructions for Windows 10 but every time I try to run psql it asks for a non superuser password which I haven't created. How do I make it ask for the superuser without using psql -U postgres command every time? Or how can I set/change a password for a non superuser? I've tried using ALTER ROLE to change the password but get role [username] does not exist as an error message.
By default, psql tries to use your OS username as a database username. Presumably this role hasn't been created in your database, hence the "does not exist" error.
You can override this default by setting the PGUSER environment variable.

Cloud SQL - PostgreSQL - Import failed due to the lack of superuser permission

I'm migrating all the role from my PostgreSQL hosted in GCE VM to Cloud SQL by generating dump file
sudo -Hu postgres pg_dumpall -U postgres --globals-only --file=globals.sql
When I import the same(globals.sql) in Cloud SQL I came across below error:
exit status 3 SET SET SET CREATE ROLE ERROR: must be superuser to alter superusers
Note:
I used postgres user to import this dump file to the cloud sql database.
I'm curious is there any other way to tackle this since postgres user does not have superuser privileges?
I tried executed one query from globals.sql file using cloud shell, below is the output:
postgres=> CREATE ROLE vipinm;
CREATE ROLE
postgres=> ALTER ROLE vipinm WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION NOBYPASSRLS;
ERROR: must be superuser to alter superusers
Thanks in advance!
The psql documentation says:
psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own occurs (e.g., out of memory, file not found), 2 if the connection to the server went bad and the session was not interactive, and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set.
So don't set ON_ERROR_STOP.
The error means that you cannot execute the following line from your dump:
ALTER ROLE postgres WITH SUPERUSER INHERIT CREATEROLE CREATEDB LOGIN REPLICATION BYPASSRLS;
That is fine, and you can ignore the error.
This is kind of a bug. As a non-superuser, you can't even reiterate that another role is still not a superuser, as even mentioning anything about superusers even when it would have no effect throws an error. You can get around this by creating the role in its final state, rather than doing the CREATE then ALTER dance that pg_dump likes to do.
CREATE ROLE vipinm WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION NOBYPASSRLS;
Alternatively, you could remove from the ALTER statement all the attributes that don't cause any change but merely reiterate the current state of things, leaving:
ALTER ROLE vipinm WITH LOGIN;

postgres main role is not postgres, how can I set it up to postgress

In terminal, when I write psql it login to "Coyr". I want "postgres" to be the main user and have all the attributes. How can I accomplish that?
I guess you ran initdb as user coyr without the -U option, and now you want to rename the bootstrap superuser.
That is easy:
drop the user postgres you created
create a new superuser maxi
connect as maxi and run
ALTER ROLE coyr RENAME TO postgres;
connect as postgres and
DROP ROLE maxi;
By renaming coyr will have lost its password, so if you need one, you have to set it again.

Fail to connect PostgreSQL DB from my Linux User

I am new to technologies , please do not judge my question too strong :).
I installed in My Ubuntu 18.04 PostgreSQL 10.7. To be able to enter my DB I need to enter the following commands from my terminal. sudo -u postgres psql.
Is there any shortened way where I can connect it from my Ubuntu User account. For example. if I input psql it will open database environment where I can type PostgreSQL commands.
Thank you.
Just execute this command in your terminal :
alias psql='sudo -u postgres psql'
So the next time, you input psql and execute, you will be in database environment.
I see two options:
1) Create alias for this command sudo -u postgres psql .
2) Go to psql and create new superuser and database for it:
CREATE ROLE username SUPERUSER;
ALTER ROLE username WITH LOGIN;
CREATE DATABASE username;
You shouldn't be using the superuser account for your normal database work. That is as if you were using root for everything in Linux.
You need to create a regular user with the privileges to create or modify tables in your database. This can be done by granting the user all privileges on the database (which is not the same as making that user a superuser) or make that user the owner of that database.
As documented in the manual psql tries to connect to a database with the name of the current Linux user and with a database user with the name of the current Linux user. So if you want to keep things simple create a user with your regular Linux user's name and an database that is owned by that user:
create user rob password 'somepassword';
create database rob owner = rob;
Assuming your Linux user is rob, then all you need to do is:
psql
and you are connected to a database where you can create and manage tables.
Depending on how you installed Postgres, you might need to adjust pg_hba.conf to allow rob to log in directly.
Again: please do NOT use the superuser account for your normal work.

Superuser expired and is the only user

I'm using Postgres 9.5 on Windows 7.
I have only one user postgres and I defined an expiry date for that user.
Now I'm trying to connect after is expired and I can't, so does anyone know how
to cancel the expiry from the superuser, so I don't have to reinstall Postgres.
I tried to edit pg_hba.conf to allow trust for postgres but still the same problem.
Start the server in single-user mode to fix the faulty "expiry date" with:
ALTER ROLE postgres VALID UNTIL 'infinity';
The manual:
The postgres command [...]
When invoked in single-user mode from the shell, the user can
enter queries and the results will be printed to the screen, but in a
form that is more useful for developers than end users. In the
single-user mode, the session user will be set to the user with ID 1,
and implicit superuser powers are granted to this user. This user
does not actually have to exist, so the single-user mode can be used
to manually recover from certain kinds of accidental damage to the
system catalogs.
Bold emphasis mine.
Aside: It's a pretty "creative" idea to let the superuser postgres expire. IOW: don't.