How to connect to Postgres 13 by user with some database - postgresql

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

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

Can not connect to a postgres database in a docker container from my local machine

I have a strange problem with docker and postgres. I can not connect from my local machine to psql inside the docker container.
First of all I created my container with this command:
docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
After that I executed the container with following command:
docker exec -it my-postgres bash
Inside the container I started psql with the postgres user:
psql -U postgres
And I created a new database:
CREATE DATABASE mytestdb;
Everything works inside the container, but if I want to connect to it from my machine it does only show the database from my machine. I tried this to connect to postgres:
psql -h localhost -p 5432 -U postgres
Which should me show something like this:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
mytestdb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
But instead I get this:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------------+-----------------+----------+---------+---------+----------------------------------
user | user | UTF8 | C | C |
postgres | user | UTF8 | C | C |
template0 | user | UTF8 | C | C | =c/user +
| | | | | user =CTc/user
template1 | user | UTF8 | C | C | =c/user +
| | | | | user =CTc/user
(4 rows)
(END)
In every tutorial, I saw they did it, but for me, it doesn't work. So please help me.
The problem is when you creating the db.
CREATE DATABASE mytestdb you forgot to add ; the command should be like CREATE DATABASE mytestdb;
And then you have your db and you can access it from inside the container or your host with these commands

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

Postgres: Create database with superuser and password

I created a Postgres database this way:
createdb massive
Then I added a role:
sudo -iu postgres
psql -d massive
CREATE ROLE rob WITH SUPERUSER CREATEDB CREATEROLE LOGIN ENCRYPTED PASSWORD 'password';
I quit psql with \q, then exit user postgres.
Now I try to login:
psql -U rob -d massive -h localhost -W
When entering password at the password prompt, I get this:
psql: FATAL: password authentication failed for user "rob"
Update:
\lin psql returns this:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
massive | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | 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
\du returns this:
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication | {}
rob | Superuser, Create role, Create DB | {}

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?