Cannot use psql "sudo: unknown user" "sudo: unable to initialize policy plugin"

I've been working on a bot for Discord which utilizes a PostgreSQL database and I'm trying to install it on a Vultr server (Ubuntu 20.04). Everything is working fine until I get to the part where I'm trying to create the database inside the server and I've been trying to follow multiple guides on how to set it up, but whenever I try to run any postgres/psql commands such as sudo -u postgres createuser --interactive I get an error saying could not change directory to "/root": Permission denied

When I try to log into a user I've created by using sudo -u araraura psql I get errors saying sudo: unknown user: araraura and sudo: unable to initialize policy plugin.

I've been at this for a day searching and looking for solutions on the internet, but I just couldn't find any solution that worked for me.

4

2 Answers

I had the same issue on ubuntu 20.04 version running postgresSQL 12 version and this is how is SOLVED:

  • open the file pg_hba.conf in your favorite editor
  • Example: sudo nano /etc/postgresql/12/main/pg_hba.conf

then you will see somethin like this:-

#local DATABASE USER ADDRESS METHOD [OPTIONS]
#host DATABASE USER ADDRESS METHOD [OPTIONS]
#hostssl DATABASE USER ADDRESS METHOD [OPTIONS]
#hostnossl DATABASE USER ADDRESS METHOD [OPTIONS]
#hostgssenc DATABASE USER ADDRESS METHOD [OPTIONS]

remove the # from the first line which un-comments the local, and replace with this below:-

local all all trust

Local is how you manage Unix domain socket connections, now you can connect your database without password prompt and if you want to be asked password when connecting to database change the "trust" method to "md5", and run the following:

psql -U your_username db_name 

To login psql with new created user role, you’ll need a Linux user with the same name as your new Postgres role.

To add the newly created user as linux user

  • sudo adduser <new_user_role>

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like