I have installed PostgreSQL server in Google Colab as follows :
# Install postgresql server
!apt update > /dev/null
!apt install postgresql > /dev/null
!service postgresql start
and have then configured the 'postgres' userid and database as follows :
# Setup a password `pass` for username `postgres`
!sudo -u postgres psql -U postgres -c "ALTER USER postgres PASSWORD 'pass';"
#
# Setup a database with name `praxis` to be used
!sudo -u postgres psql -U postgres -c 'DROP DATABASE IF EXISTS praxisdb;'
!sudo -u postgres psql -U postgres -c 'CREATE DATABASE praxisdb;'
Subsequently, I have created a table, inserted data and run the select command
!psql -h localhost -p 5432 -Upostgres -W -dpraxisdb -c 'create table dept ... ;'
!psql -h localhost -p 5432 -Upostgres -W -dpraxisdb -c "INSERT INTO Dept ... ;"
!psql -h localhost -p 5432 -Upostgres -W -dpraxisdb -c "select * from dept;"
All this works perfectly but each time, I am prompted to enter the password. I wish to avoid having to enter the password each time. Based on what I read in the documentation on using password files, I created a file ~/.pgpass as follows :
!echo "localhost:5432:praxisdb:postgres:pass" > ~/.pgpass
!chmod 0600 ~/.pgpass
Now, when I execute any command, eg.
!psql -c "select * from dept;"
I get the error
psql: error: FATAL: role "root" does not exist
Where does this role root come from? I looked at the file ~/.pgpass and noted that its owner and group is root and I changed that to postgres using chown, chgrp, but that does not solve the problem. What else should I do in this case to solve the problem.
The version of Postgres and the OS is as follows :
!sudo -u postgres psql -V
psql (PostgreSQL) 12.13 (Ubuntu 12.13-0ubuntu0.20.04.1)
CodePudding user response:
You misunderstand how the password file works. You still have to specify host, port, database and user in your connection request. The client library then searches the matching entry in the password file and reads the password.