Can't access superuser in postgresql - postgresql

I can't seem to access my superuser account for my posgresQL database.
Using the command:
psql -U postgres
I sucessfully login into the user 'postgres'. However, this is not the default superuser. Doing:
=# \du
I get:
List of roles
Role name | Attributes | Member of
-----------+-------------------------------------+-----------
pgsql | Superuser, Create DB | {}
postgres | Create role, Create DB, Replication | {}
So 'pgsql' appears to be my default superuser.
When trying:
psql -U pgsql
I get the following error:
psql: FATAL: database "pgsql" does not exist
I changed the pg_hba.conf file to the following:
# Database administrative login by Unix domain socket
local all all trust
and also tried:
# Database administrative login by Unix domain socket
local all pgsql trust
but I still get the same error regardless, that database 'pgsql' does not exist.
Any help would be greatly appreciated, I need to be able to access the database's superuser.

just define database:
psql -U pgsql -d postgres
if you get error that postgres database does not exist, connect as postgres and list databases with \l

Related

Username seen as database in psql command?

This question is similar to create database using psql in shell script takes username as db name, but since that doesn't have an accepted answer I'm asking it again here. I've created a user with username myuser and password mypassword:
> psql
psql (11.5)
Type "help" for help.
kurt=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
kurt | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
myuser | | {}
kurt=#
However, if I do psql --username=myuser, I get an error that database "myuser" does not exist:
> psql --username=myuser
psql: FATAL: database "myuser" does not exist
I'm a bit confused by this error message, because according to psql --help, this is a user name, not a database name:
Connection options:
-h, --host=HOSTNAME database server host or socket directory (default: "local socket")
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "kurt")
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
Any idea what I'm doing wrong? Here is the version of psql I'm using:
> psql --version
psql (PostgreSQL) 11.5
Quote from the manual
The default user name is your operating-system user name, as is the default database name
If you specify a username, that username is then also assumed as the default for the database name.
After creating your new user, first you have to login using postgres user and create new database similar to your new created user. ex: if my new user is "david", I must create also database called "david" using posgtres user.After this you can now login using your new user created.

Postgres: psql: FATAL: database "root" does not exist in windows 10

Please check below command which I had run:
C:\Windows\system32>psql -U root
Password for user root:
psql: FATAL: database "root" does not exist
If you don't explicitly specify which database you want to connect to, psql will use the database with the same name as the database user.
Since you specified database user root, psql tries to connect to a database of that name, but the database doesn't exist.
Try specifying an existing database:
psql -U root -d mydb
In case you don't know which database to use, or if you never created a database, you can always use the postgres database.

How can I create a Postgres 11 user/role with a different name than the OS-user?

I'm a newbie to PostgreSQL and I'm trying to create a user/role on Postgres 11 (running on CentOS7) which is named (e.g.) "json_docs_admin" (same for password) that I can use under the OS user (e.g.) "app_user" to connect locally to the DB.
I have tried connecting to Postgres using "postgres" user and the command "psql -d template1" and ran the following commands:
template1=# create role json_docs_admin with login password 'json_docs_admin';
CREATE ROLE
template1=# create database json_docs owner=json_docs_admin;
CREATE DATABASE
template1=# GRANT ALL PRIVILEGES ON DATABASE json_docs TO json_docs_admin;
GRANT
I have changed the /var/lib/pgsql/11/data/pg_hba.conf so that local connections can be established on the server, and I've restarted the postgresql service.
Then, back under "app_user" user, when I try to connect to the database, I get the following errors:
$psql -d json_docs -U json_docs_admin
Password for user json_docs_admin:
psql: FATAL: database "json_docs" does not exist
Could anybody let me know what am I doing wrong?
Thanks.

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/)

Cant create postgres base type (with hstore/pgcrypto extensions) on amazon RDS

We are moving from our own native postgres installation on an EC2 instance to amazon RDS for postgres. And the data migration is done via dump and restore like this:
pg_dump -U postgres test1_main> test1_main.dump
psql -U postgres -h <rds end point> -p 5432 test1_main < test1_main.dump
On restore, we see failures saying:
CREATE SCHEMA
ALTER SCHEMA
ERROR: must be owner of language plpgsql
ALTER LANGUAGE
SET
CREATE TYPE
ALTER TYPE
ERROR: must be superuser to create a base type
ERROR: permission denied for language c
ALTER FUNCTION
ERROR: permission denied for language c
ALTER FUNCTION
ERROR: must be superuser to create a base type
.....
ERROR: permission denied for language c
ERROR: function pgcrypto.armor(bytea) does not exist
ERROR: permission denied for language c
And so on!
We have added the hstore and pgcryptop extensions to template1 database on our Amazon RDS instance[1] so we have these extensions but we cant create a type because creating a base type in postgres 9.3 needs superuser[2]. postgres user is disallowed to do this and the amazon RDS documentation says:
When you create a DB instance, the master user system account that you
create is assigned to the rds_superuser role. The rds_superuser role
is similar to the PostgreSQL superuser role (customarily named
postgres in local instances) but with some restrictions
rdsadmin is the superuser but I cant log in with rdsadmin.
oncall#ip-10.144.xx.xxx:~> psql -U rdsadmin -h <rds end-point> -p 5432
psql.bin: FATAL: pg_hba.conf rejects connection for host "10.144.xx.xxx", user "rdsadmin", database "rdsadmin", SSL on
FATAL: pg_hba.conf rejects connection for host "10.144.xx.xxx", user "rdsadmin", database "rdsadmin", SSL off
Please help.
[1] http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#SQLServer.Concepts.General.FeatureSupport
[2] http://www.postgresql.org/docs/9.3/static/sql-createtype.html