Z
Z
zlodiak2019-11-23 21:06:03
PostgreSQL
zlodiak, 2019-11-23 21:06:03

How to log in as a user?

I installed postgres9. Further I work with it through the command line. My OS is linux mint 18.
After installing postgres, the postgres user was created in the OS, as indicated by the entry in /etc/passwd:

postgres:x:123:132:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash

Now I would like to create a postgres user and run under it instead of superuser. To do this, I logged into postgres as a superuser:
sudo -u postgres psql
and created a new user, a new database, gave the user rights to this database:
postgres=# CREATE USER user1 WITH PASSWORD 'qwerty1';
CREATE ROLE

postgres=# CREATE DATABASE database1;
CREATE DATABASE

postgres=# GRANT ALL PRIVILEGES ON DATABASE database1 to user1;
GRANT

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 database1 | postgres | UTF8     | ru_RU.UTF-8 | ru_RU.UTF-8 | =Tc/postgres         +
           |          |          |             |             | postgres=CTc/postgres+
           |          |          |             |             | user1=CTc/postgres

Next, I log out of the superuser account and try to log in as user1, but it doesn't work:
postgres=# \q
(ven) [email protected] ~/.MINT18/code/python/project0/ven/bin $ sudo -u user1 psql
sudo: неизвестный пользователь: user1
sudo: не удаётся инициализировать модуль политики

Please tell me what is the problem and can it be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-11-23
@zlodiak

sudo - Run a command as an OS user.
An OS user has nothing to do with a database user. You have not created an OS user, the sudo answer is natural.
You don't need sudo to use psql. You need pg_hba.conf settings and psql itself. For example,psql -U user1 database1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question