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

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.

Related

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

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.

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.

How to login to PostgreSQL with different username than "postgres" and how to encrypt specific columns?

I am absolutely new to PostgreSQL. I have the following doubts:
I am able to login to postgresql using
sudo -u postgres psql postgres
I have created a test_db database and inside it a table called test_tbl. I have also created a user test and given it the below privileges:
GRANT CONNECT ON DATABASE test_db TO test;
GRANT SELECT, INSERT, UPDATE, DELETE ON test_tbl TO test;
How do I login to the test_db database as test user? I am only able to login as postgres. If I try login as test I get the below error-
ubuntu#ip-10-81-1-44:~$ psql -U test -d test_db
psql: FATAL: Peer authentication failed for user "test"
How can I encrypt a specific column in table test_tbl, just for user test and no other user?
You are not required to sudo change your user, you can specify that on connect using the -U flag, like so:
psql -U test -d test_db
this will try to connect you to the database test_db as user test and if a password is required you will be prompted, other flags such as host, port etc are listed here
https://www.postgresql.org/docs/current/app-psql.html
You will have to create an entry in pg_hba.conf that allows the user into the database. Don't forget to reload PostgreSQL.
Read the documentation about client authentication for more.
To encrypt a column, you need application code that does the encryption and decryption.
Perhaps you can get what you want with column privileges:
GRANT SELECT ON test_tbl TO test;
GRANT SELECT (col1, col2, ...) ON test_tbl TO PUBLIC;
Here you list all columns except the one you want to hide.
Then only user test can SELECT that column.
An alternative is to restrict SELECT to test only and provide to the others a view that shows everything except the hidden columns.

createdb: database creation failed: ERROR: permission denied to create database

I am pretty much confused about root user,super user,user and permissions! I am not able to create a database inside user "athleticu". Following are the commands I used:-
athleticu#ip-172-30-4-103:/home/ubuntu$ createdb -T template0 simple_db1
createdb: database creation failed: ERROR: permission denied to create database
athleticu#ip-172-30-4-103:/home/ubuntu$ sudo createdb -T template0 simple_db1
sudo: unable to resolve host ip-172-30-4-103
createdb: could not connect to database template1: FATAL: role "root" does not exist
Please somebody clarify my doubts and tell me what should I write!
Hey I have already solved this. What you have to do is to first login as postgres user as follows:
$ su postgres
$ psql
postgres=# alter user athleticu createdb;
ALTER ROLE
Hope it helps you :)
Type \du in psql and you will see a list of all the registered users and what type of privileges each one has.
In order to grant privileges to the user which is logged in (eg 'user1'), I had to sign out and log in using one of the superuser roles in that list (eg. 'user2'), using the following command:
psql -U 'user2' -h localhost 'database2'
where 'database2' is the name of the one that specific superuser 'user2' has privileges to.
Once you are logged in as a superuser, you can grant privileges to 'user1' by:
ALTER ROLE user1 WITH CREATEDB
or
ALTER ROLE user1 WITH SUPERUSER
Then sign in again as user1, who is now a superuser.
This blog was helpful as well as this link.
Currently, this worked for me:
sudo su postgres
psql
ALTER USER username WITH CREATEDB;
\q
exit
The root user is an account on the system independent from Postgres. There is only one root user.
A superuser is an account in Postgres with access to everything. There may be many superusers.
System accounts and Postgres accounts are different things, although unless you specify a Postgres username when you connect to the database (through utilities like psql, createdb, dropdb, or otherwise), it will use the current system user's name in hopes that there is a corresponding Postgres account with the same name. The root user does not, by default, have a corresponding account in Postgres.
When you install Postgres on *nix, it creates both a superuser named postgres and a system user named postgres.
Therefore, when you need to do something with Postgres as the built-in superuser, you have two options:
You may sudo su - postgres to become the postgres system user and execute your command (createdb, psql, etc). Because the system user has the same name as the database superuser, your command will connect as the appropriate account.
You may specify the username to execute as with the -U switch, eg psql -U postgres ....
Depending on your Postgres server's authentication settings, you may be required to enter a password with either or both connection methods.
What you can do when you have fresh installation of PostgreSQL is create your user with some rights (see createuser documentation):
my-user> sudo su - postgres -c "createuser <my-user> --createdb"
This will allow my-user to create DBs just like so:
my-user> createdb <my-db>
If you want the my-user to be able to do anything just use the --superuser flag instead:
my-user> sudo su - postgres -c "createuser <my-user> --superuser"
I got the same error and I found out that the reason was that I was trying to create a database outside of psql as a user which did not exist for postgresql. I found out about it and solved it by taking the following steps:
In my terminal I logged in as postgres user (the root user by default for postgresql) by typing sudo -u postgres psql
While inside the psql I typed \du to see all users and their privileges. I found out that I had only one user (the postgres one) and I had to create another superuser which had the same username as my Linux user (george)
I typed (still inside psql) CREATE USER george SUPERUSER; and this way I created a new super user called george.
I exited psql (by typing \q) and I was now able from outside psql, meaning from my terminal, to run created db <database name> with no issues at all.
Error ? You are trying to perform database actions( Creating Database, creating Roles) using a user that doesn't have the permission for those types of actions you are trying to perform.
solution ? Simply login to your database on the command line, i.e for PostgreSQL one will use "sudo -u postgres psql", then confirm that users specific assigned roles using the command "\du", most probably he/she doesn't have the necessary permissions to perform the actions you wanted. Then simply assign the roles you want the user to perform ,i.e create Database or simply make user "Superuser" by following along(https://chartio.com/resources/tutorials/how-to-change-a-user-to-superuser-in-postgresql/)

How do I rename the default postgres superuser to "root"?

I currently log in to PostgreSQL using psql -U postgres. How do I rename postgres user to root?
If I am already logged in as postgres then trying ALTER USER postgres RENAME TO root will say ERROR: session user cannot be renamed.
Is it possible to rename the user without logging as postgres user? I don't think I have any other superuser since this is a fresh install of PostgreSQL.
By the way, I am running Gentoo on Amazon EC2.
You should be able to just create a new postgres superuser called root by logging in as the postgres user and (at the shell) typing;
createuser --superuser root
psql> create database root owner root
After that, when logged in as root, you should be able to do what you want with the postgres user.
You can try
update pg_authid set rolname ='root' where rolname = 'postgres';
But be aware, that munching the system catalogues by hand is always a little dangerous.
What about:
ALTER ROLE postgres RENAME TO root;
using a different superuser role?
In order to rename current user oldname to newname, first log in as oldname and create a temporary superuser as such:
CREATE ROLE temp LOGIN SUPERUSER PASSWORD 'mytemporarypassword';
Log out, then log in using the temp superuser and rename the oldname user. You will have to reset the password for that user at the same time, as PostgreSQL will tell you: "MD5 password cleared because of role rename".
ALTER ROLE oldname RENAME TO newname;
ALTER USER newname WITH PASSWORD 'My-Mega-5ecure-P4ssw0rd';
Now log in using the newname user and remove the temporary user that we created previously:
DROP ROLE temp;