How to remove a postgresql user - postgresql

I followed this tutorial and made a typo where I was supposed to create a user for my django apps to connect as;
I was supposed to run su - postgres -c "createuser www-data -P" but I ran su - postgres -c "createuser www-dtata -P".
I dont want to proceed until I remove that user, which I don't know the command for. I found and tried DROP USER after searching around, but the terminal returned -su: DROP: command not found.

Run sudo su - postgres -c "dropuser www-dtata"

You can use dropuser console tool (see https://www.postgresql.org/docs/current/static/app-dropuser.html):
su - postgres -c "dropuser www-dtata"
Or use DROP USER SQL query (see https://www.postgresql.org/docs/current/static/sql-dropuser.html):
sudo -u postgres psql -c 'DROP USER "www-dtata";'
These 2 approaches do the same thing. In SQL version, you also need to use double quotes around DB user name, due to - in it.

First run the command
sudo su
Enter the user password for root access.
Then run the below command
su - postgres -c "dropuser www-dtata"
No password will be prompted

Related

How to run a postgres command: could not identify current directory

I am able to run psql by doing the following:
Davids-d david$ psql --u postgres
Password for user postgres:
psql (9.4.18)
Type "help" for help.
postgres=#
However, when I run the following command, I get an error:
Davids-iMac:datadocs david$ sudo -u postgres psql -f resources/postgresql/initdb.sql
could not identify current directory: Permission denied
What does this mean, and how would I resolve this? Note that I do have the following var set:
david$ echo $PGDATA
/Users/david/PostgreSQL/data/pg94
The issue is the sudo -u postgres.
Your shell is running as you, but you're running the command as the postgres user. It does not have permission to see the file or even be in the current directory.
We can eliminate psql from the equation by just trying to read the file as the postgres user with sudo -u postgres cat resources/postgresql/initdb.sql. You should get the same error.
There's two things you have to do...
cd to a directory that the postgres user can be in.
Put the file in a place the postgres user can access.
/tmp, for example.
Your command seems wrong, try this:
sudo psql -U postgres -f resources/postgresql/initdb.sql

What is the role of -s flag in creating user

I am trying to a create a user in postgres, I did the following.
sudo -u postgres createuser mystore
But I found out that I should use -s flag while creating the user, So my question is what is role of -s flag while creating user.
And I tried to remove the user by the following steps
sudo -u postgres psql
drop user mystore
Then tried to create the store with the -s flag, it says
role "mystore" already exists.
How to handle this
createuser -s will give the new user superuser privileges. As with most command-line tools in Linux, you can get a description of each flag by running createuser --help.
The problem in psql appears to be a missing semicolon after your drop command. psql supports multi-line statements, so hitting Enter will simply add a new line; it won't submit the command to the server until it sees a semicolon terminator.

Script to create postgres user with whoami

I'm making a script to automate the process to install and create a new postgres user, the problem is that I will not ever know the name of the local user where the script will run, so I tried this with no results:
LOCALUSER=whoami
sudo su - postgres -c "psql -U postgres -d template1 -c \"create user ${LOCALUSER} with createdb;\""
It creates a new user but named "whoami", as you could guess, I need the current user name instead "whoami".
How can I pass the user name?
Thank you in advance!
Best Regards
Alejandro
Using:
LOCALUSER="$(whoami)"
Instead of:
LOCALUSER=whoami
Did the trick.
Source: https://unix.stackexchange.com/questions/175415/forward-function-and-variables-into-sudo-su-user-eof

How to alter role from command line in postgresql?

I'm trying to build a setup script to automate the development environments creation, but I'm having trouble both trying to pipe or using the -c modifier for psql.
I've tried:
sudo su postgres psql -c "ALTER ROLE postgres WITH password 'pass'"
and
sudo su postgres psql -c "ALTER ROLE postgres WITH password 'pass';"
Both of which say "ALTER: command not found"
I've also tried pipe, but I'm not able to combine it with su correctly
eg: I tried something like
sudo su postgres echo "ALTER ROLE postgres WITH password 'pass'" | psql
But postgres can't execute "echo"
And:
echo "ALTER ROLE postgres WITH password 'pass'" | sudo su psql
Which just doesn't work.
So, my first question is: how can I execute this simple command from a sh file?
And the second one, less related: how can I use different users in the commands chained with pipe?
What's wrong is the lack of -c or --command for su to indicate that the rest of the line is a command.
But su is not needed anyway, because there's already sudo. Do this instead:
sudo -u postgres psql -c "ALTER ROLE postgres WITH password 'pass'"
If you are just like me, make sure you are not running this from:
bash-4.2$
but:
postgres=#

What's the default password in postgres?

I install postgres on tutorial digital ocean, but when I paste `
sudo su – postgres
bash show me
[sudo] password for postgres:
and want to know what I do?
what's the password?
sorry for the bad English
`
That prompt suggests you're probably already the postgres system user, so you don't need to sudo. Just run the command you're trying to run directly.
In general, don't use sudo su - postgres. It's pointless. Just:
sudo -u postgres psql
or whatever directly. To get a shell:
sudo -u postgres -i
There is never any need to use su - with sudo.