What's the default password in postgres? - postgresql

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.

Related

shell script having embedded password inside it is not working as expected

I have developed a shell script whose job is to take the dump of postgres DB. Below is the snippet:
#!/bin/sh
today=$(date +"%Y-%m-%d")
yes "password" | sudo -S sudo su - postgres <<EOF
/usr/pgsql-11/bin/pg_dump -U postgres -d db_name > /home/db_backup/db_name_$today.sql
EOF
exit
However, this script is NOT running because of the below reason:
[sudo] password for user: Sorry, Try again
However, when I use sudo su - postgres and then provide password, it is working as expected. And interestingly, if now I run the above shell script after the login, it runs absolutely fine.
What I am missing here.
It is dangerous to store passwords in scripts, so please do not do it.
Modify your /etc/sudoers file by running sudo visudo and adding a line like this at the bottom:
%sudo ALL=(postgres) NOPASSWD: /usr/bin/psql
This allows anyone with sudo permission to run /usr/bin/psql to postgres on any host (ALL) with no password.
Now your script should work this way:
#!/bin/sh
today=$(date +"%Y-%m-%d")
sudo -b -n -H -u postgres /usr/pgsql-11/bin/pg_dump -U postgres -d db_name > /home/db_backup/db_name_$today.sql
Make sure postgres can write to the directory /home/db_backup/.

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

Why postgresql on mac asks me for password after fresh install?

I just installed postgresql on a Macbook with brew install postgresql. Then I try to psql, but it requires password and then show psql: FATAL: password authentication failed for user "<myname>".
I have not set up anything, and inputting my mac password does nothing. What should I do now?
So your username probably does not exist, as the default username that ships with the db is postgres.
Further, I was prevented from the submission of an empty password, which is blank by default for the postgres user.
You might try
cd ~/
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
Password: YOUR_LOGIN_PWD_HERE (required for sudo)
and then to use
psql -U postgres
password: postgres
I'm not 100% sure of which SO answer I got this from, perhaps here. Hope this helps.
The above did not work for me.
The below steps worked for me:
Step 1: Uninstall Postgres using the following steps:
sudo /Library/PostgreSQL/10/uninstall-postgresql.app/Contents/MacOS/uninstall-postgresql
PS: my postgres version is 10
Step 2: Remove Postgresql user
System Preference > userse & Groups > Unlock > remove postgresql user by clicking "-"
Step 3: Remove existing databases
rm -rf /usr/local/var/postgres/*
Step 4: Install and Start Postgres using brew
brew update
brew install postgresql
brew services start postgresql
Step 5: Create database
initdb /usr/local/var/postgres -E utf8
You can start accessing postgres
psql -h localhost -d postgres
The answer by Pramida almost worked for me... the difference is I was using 9.6 Postgres.
So I ran:
sudo
/Library/PostgreSQL/9.6/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh
and somehow that got rid of my username and almost all of postgres user. I think
I then blew away the directory
sudo rm -rf /Library/PostgreSQL/9.6
And then I installed using brew above.
in my case macboook big sur v 11 you should create /var/postgresql#12 in Mackbook/usr/local
and open terminal in /opt/homebrew/Cellar/postgresql#12/12.8/bin and run
/opt/homebrew/Cellar/postgresql#12/12.8/bin/initdb -D /usr/local/var/postgresql#12
then run in terminal
echo 'export PATH="/opt/homebrew/Cellar/postgresql#12/12.8/bin:$PATH"' >> ~/.zshrc
then
psql -h localhost -p 5432 -d postgres
and enjoy creating user
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

How to remove a postgresql user

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

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=#