I have created a postgres database in postgres named "databaseName". Now I can access this data2database through
su - postgres
and then typing my password
Then I enter into the database through: psql databaseName
I have created users of this database through:
createuser -P userName1
Now I dont want the users of the database to access the database as root user. Now when the users try to login into postgres as
su - postgres -u userName1
or through
psql databaseName -u userName1
I get error...can someone guide me as to how the users can get access to postgres and database without being root user?
Configure pg_hba.conf to accept peer connections over unix sockets or ident connections over host (tcp) connections. If you prefer you can use md5 to use password authentication instead.
By default psql will connect with the same username as your local OS username; this can be overridden by the -U flag, eg:
psql -U myusername thedatabase
Note that it's -U not -u (it's upper case).
This is all covered in detail in the PostgreSQL documentation; see:
Client Authentication
pg_hba.conf
psql
BTW, if you want to run commands as the postgres user, rather than su'ing you can just write:
sudo -u postgres psql
Related
If I create a DB user with
"sudo -u postgres psql -c "CREATE USER guacamole PASSWORD 'PW'; GRANT ALL PRIVILEGES ON DATABASE guacamole TO guacamole"
this user disappears after reboot. How can I make this user/command persistent, so that I do not need to recreate after reboot
thanks
I need to use root user to run scripts at crontab, for example to read and write on all /home folders.
But something that I need to do also in the shell script is to run psql. Problem:
my user (me = whoami and not is root) can run for example psql -c "\l"
the root user not works (!) with psql -c "\l"... And error not make sense "psql: error: could not connect to server: FATAL: database "root" does not exist".
How to enable root to run psql?
PS: looking for a kind of "GRANT ALL PRIVILEGES ON ALL DATABASES TO root".
root is allowed to run psql, but nobody can connect to a database that doesn't exist.
The default value for the database user name with psql is the operating system user name, and the default for the database is the same as the database user name.
So you have to specify the correct database and database user explicitly:
psql -U postgres -d postgres -l
The next thing you are going to complain about is that peer authentication was denied.
To avoid that, either run as operating system user postgres or change the rules in pg_hba.conf.
I want to export table from ma database using remote access (ssh) and psql (\copy command) but failed
to resume:
I have a database named mydatabase and user named myuser granted on this database
table I try to extract is named mytable
I connect to my remote server using Putty
once connected, I run psql : sudo -u postgres psql (I tried to connect using myuser but failed because mysuser is unknown ???)
I connect to my database : \c mybase
I run \copy mytable TO '/home/path/to/my/file/file.txt'
and get error message: '/home/path/to/my/file/file.txt' : Permission denied
as I said, I tried to connect using myuser thinking it could solve permission issue but don't know why it failed...
psql -d mydatabase -U myuser
psql: error: could not connect to server: FATAL: Peer authentication failed for user "myuser"
Let's assume the operating system user you connect with using Putty is x.
Since you run psql as user postgres using the sudo command, it is the operating system user postgres that needs permissions to write /home/path/to/my/file/file.txt.
So user x must give postgres the required permissions.
I am trying to learn SQL. I have picked up postresql . now I hae a query myself. How can specify login role(user) or switch the role ?
I had create new users like this:-
in zsh shell with postgres user
% createuser --interactive sqltest01
and
% psql
postgres=> create user sqltest02 with password '1';
CREATE ROLE
now I have 3 users or roles
postgres=> \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
sqltest01 | Create role, Create DB | {}
sqltest02 | | {}
How can I login with these users ?
I usely login like this :-
% sudo su postgres
% psql
which logs me in with user postgres
\conninfo
You are connected to database "postgres" as user "postgres" via socket in "/run/postgresql" at port "5432".
how can I login with sqltest01 or sqltest02 ?
If you are logged into the same computer that Postgres is running on you can use the following psql login command, specifying the database (mydb) and username (myuser):
psql -d mydb -U myuser
If for some reason you are not prompted for a password when issuing these commands, you can use the -W option:
psql -d mydb -U myuser -W
I have a shell script which runs on deployment and I have these lines:
# Database
createdb $DBNAME
createuser -D -A $DBNAME
However, in my logs I get this error:
createdb: could not connect to database postgres: FATAL: Ident
authentication failed for user "root"
createuser: could not connect to database postgres: FATAL: Ident authentication failed for
user "root"
Would anyone mind telling me what is going wrong here and how I can correct my lines. Surely root should have permission to do this?
the best way to do this, especially if you want your script to be portable, is:
su --login postgres --command "createdb $DBNAME"
this should be safer, more secure, and more portable than using -U. i do it this way in all my posgreSQL scripts. you might find it a useful technique. obviously it still needs to be run as a root user (e.g. with sudo).
If your DB is secured, you need to connect as a DB user, not as a user of the OS. For example:
createdb -U dbrootuser -W $DBNAME
See this link for full syntax