POSTGRESQL : Old owner present on new installation - postgresql

I want to flush this DB so that the old owner doesn't exist anymore and I want to be the superuser.
I am using an ex-colleagues laptop ( mac )
PostgreSQL ( psql ) was installed via Homebrew ( 9.6 ) which I have removed as I required ( 9.5 ).
A few "perhaps" issues I've noticed:
psql9.5 is now installed and when I attempt to login I receive the following FATAL: role "MY NAME" does not exist
eventually login and when I "\l" I see the following:
DB_NAME-> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------------+----------+-------------+-------------+-------------------------------
DB_NAME | DB_NAME | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
postgres | EX COLLEAGUE | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | EX COLLEAGUE | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/EX COLLEAGUE +
| | | | | EX COLLEAGUE=CTc/EX COLLEAGUE
template1 | EX COLLEAGUE | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/EX COLLEAGUE +
| | | | | EX COLLEAGUE=CTc/EX COLLEAGUE
when I run:
DB_NAME=> \du
List of roles
Role name | Attributes | Member of
--------------+------------------------------------------------------------+-----------
DB_NAME | Create DB | {}
EX COLLEAGUE | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
I can't create new Roles.
I might be over-exaggerating the severity of this - but I have no idea what to do.
I have looked at old resources and maybe I was searching incorrectly but I found no help.

Solved - had to re-install my Mac OS but was hoping for a less time consuming option

Related

Postgresql error database does not exist however it exists when listing the databases

When I drop the database I have an error that it doesn't exist however when I list the databases I can see it.
Here are the steps I am following:
sudo -u postgres psql
postgres=# \l
postgres=# DROP DATABASE IF EXISTS mydbname;
NOTICE: database "mydbname" does not exist, skipping
DROP DATABASE
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------------+---------------+----------+---------+---------+----------------------------
mydbname | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | cryptouser=CTc/postgres

How to connect to Postgres 13 by user with some database

I installed
postgres=# SELECT version();
version
--------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 13.5 (Ubuntu 13.5-0ubuntu0.21.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 10.3.0-1ubuntu1) 10.3.0, 64-bit
(1 row)
and I want to create connection for my project(java, php, what ever) so for that I followed in user guide
CREATE ROLE admin WITH SUPERUSER LOGIN CREATEDB CREATEROLE ENCRYPTED PASSWORD '111';
and right now faced with that
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
admin | Superuser, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
So, after that
CREATE DATABASE spd_talks;
and faced with
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 |
spd_talks | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 |
template0 | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
and provide all grant
GRANT ALL PRIVILEGES ON DATABASE spd_talks TO admin;
then admin was added to Access privileges column
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 |
spd_talks | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | admin=CTc/postgres
template0 | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | ru_UA.UTF-8 | ru_UA.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
then I try to connect to spd_talks db by admin user and faced with error
ivan#ivan-laptop:~$ psql -U admin spd_talks
psql: error: FATAL: Peer authentication failed for user "admin"
the same case when I try to did it by IDE
what I missed, how to correct create user with db and connect by him?
I changed
local all postgres peer
to
local all postgres md5
in
sudo gedit /etc/postgresql/13/main/pg_hba.conf
but still faced
ivan#ivan-laptop:~$ psql -U admin -d spd_talks
psql: error: FATAL: Peer authentication failed for user "admin"
UPDATE
when I changed
local all all peer
to
local all all md5
I faced with successful connection by console
ivan#ivan-laptop:~$ psql -U admin -d spd_talks
Password for user admin:
psql (13.5 (Ubuntu 13.5-0ubuntu0.21.04.1))
Type "help" for help.
spd_talks=#
but still error in ide
I resolved this missundertood by
spd_talks=# \conninfo
You are connected to database "spd_talks" as user "admin" via socket in "/var/run/postgresql" at port "5434".
and I don't now why but port is 5443, then I canged it in ide and it works

Setting up production postgres (createdb permissions)

I'm setting up postgres on DigitalOcean. I've set PG envs. The 9s are obviously redacted data. To facilitate the ssl connection, ~/.postgresql/root.crt exists.
# env | grep PG
PGPORT=99999
PGPASSWORD=9999999999999
PGSSLMODE=require
PGUSER=doadmin
PGDATABASE=auth_service_prod
PGHOST=private-db-postgresql-sfo9-99999-do-user-9999999-9.a.db.ondigitalocean.com
When calling createdb, it fails:
# createdb
createdb: could not connect to database template1: FATAL: pg_hba.conf rejects connection for host "10.999.9.99", user "doadmin", database "template1", SSL on
I can connect to psql, though. I don't know enough about configuring postgres to allow doadmin to create a db, but hopefully here is some useful info:
# psql
psql (11.7 (Debian 11.7-0+deb10u1), server 11.8)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
auth_service_prod=> \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
_dodb | Superuser, Replication | {}
doadmin | Create role, Create DB, Replication, Bypass RLS | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
auth_service_prod=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------------+----------+----------+-------------+-------------+-----------------------
_dodb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
auth_service_prod | doadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
defaultdb | doadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
DigitalOcean doesn't allow createdb from postgres, apparently. I removed the code creating databases, and created them "manually" from their GUI. The app is now only responsible for managing the schema, not the database(s).

PostgreSQL can't create user [duplicate]

This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 3 years ago.
Issue: I can create users or databases from the shell (bash, OSX) but not postgres cli. From bash I get no confirmation if successful.
If I try to CREATE ROLE in psql then I get no response and it doesn't generate any error. If I try to createuser from bash then if successful it reports back nothing, if unsuccessful then it does generate the error: "role username already exists".
Example:
Yunti-# CREATE ROLE testuser
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}
Yunti-# CREATE DATABASE testdb
Yunti-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
Yunti | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
template1 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
test | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
test5 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(6 rows)
Yunti-#
A similar thing happens when using createdb.
How can I create users and databases in postgres cli?
And is this normal to get no response to most postgres commands in bash?
Info: users and their privileges:
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}
Your statements are not executed, because you don't terminate them properly using a ;.
Quote from the manual:
A command is composed of a sequence of tokens, terminated by a semicolon (";").
And in the manual for psql:
At the prompt, the user can type in SQL commands. Ordinarily, input lines are sent to the server when a command-terminating semicolon is reached. An end of line does not terminate a command. Thus commands can be spread over several lines for clarity. If the command was sent and executed without error, the results of the command are displayed on the screen.
If you do that, you get an output like this:
psql (9.4.4)
Type "help" for help.
postgres=# CREATE ROLE testuser;
CREATE ROLE ---<<<< this tells you the statement was executed
postgres=#
I dont know if the postgres client is using other commands in OSX as it is in Linux but I assume it is the same.
This docs link shows some options for the postgres client:
It seems like "\l" lists the databases while the option you would like to see is roles and their access which is "\du".
When creating a database from within the client you should get a response in the form of "CREATING DATABASE". Maybe you are having some sort of syntax error?
I don't think users is created elsewhere.
I hope this solves some of your problems.

How to import database to PostgreSQL as root?

I want to import a database of mine to a database of my server. So, I copied my database dump file to my server's root directory and logged in and did this:
root#iWidgetServer1:~# sudo -u postgres psql -U iwidget -d iwidget -f iwidget_dump2.sql
could not change directory to "/root"
psql: FATAL: Peer authentication failed for user "iwidget"
However, iwidget is a role and has granted all priviliges for this database:
root#iWidgetServer1:~# sudo -u postgres psql -l
could not change directory to "/root"
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------------+----------+----------+-------------+-------------+-----------------------
iwidget | iwidget | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =Tc/iwidget +
| | | | | iwidget=CTc/iwidget
postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
sample_db | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template_postgis | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
(6 rows)
What am I doing wrong?
From the docs:
The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.
You're doing sudo -u postgres, but are trying to connect as iwidget.
You need to create a user named iwidget and login as this user.
This seems to be more a problem with the File permissions of the dump instead of Postgres. Have you tried moving the dump to a folder not owned by root?