PostgreSQL: Can't log into Postgres - sudo broken? - postgresql

Hey so after I ran into somes issues I decided to uninstall Postgres and re-install it (re install a newer version btw).
I'm trying to access postgres to create a new db for my Rails app so I run su postgresor su _postgres then I am asked a password but nothing that I enter works.
So I tried sudo -u postgres psql and got
sudo: unknown user: postgres
sudo: unable to initialize policy plugin
I tried also to disable SIP (see: here)
but it didn't work either. same results.
I don't know what to do. Any idea ? I am using Mac OS High Sierra 10.13.6.

Thanks to #gordon Davidson and #wildplasser 's help, I found the solution:
sudo -u myusername psql postgres

I had this same challenge when trying to access a PostgreSQL database.
When I run the command below:
sudo -u postgres psql
I get the error below:
sudo: unknown user: postgres
sudo: unable to initialize policy plugin
The issue was that I did not have the PostgreSQL database server installed on the server. The server only had a MySQL database server set up on it.
That's all.
I hope this helps

From your EC2 node issue the following command to test if database is reachable.
/opt/gitlab/embedded/bin/psql -U YourExistingUsername -d template1 -h AWS-RDS-POSTGRES-ENDPOINT

Related

PostgreSQL 13.3 installation on MacOS (Big Sur) failed with error: Failed to initialise the database cluster with initdb

Downloaded the PostgreSQL installer from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, and tried to install it on my new Macbook Pro running Big Sur.
The installation went fine, but it failed when executing the script /Library/PostgreSQL/13/installer/server/initcluster.sh. I traced to the following script:
su - $OSUSERNAME -c "$INSTALLDIR/bin/initdb --pwfile $INSTALLDIR/installer/server/initdbpw.$$ --encoding=utf8 -A scram-sha-256 -U \"$SUPERNAME\" -D \"$DATADIR\""
Apparently, su - postgres does not work because the new service account postgres is not enabled for login. Maybe this script should use sudo -u postgres instead of su - postgres.
I got it working by updating the service account postgres as follows, so it is enabled for login:
sudo dscl . -passwd /Users/postgres 'MyNewPassword'
sudo dscl . -read /Users/postgres
After you verify that the password of the account is no longer "*", you can uninstall and reinstall PostgreSQL, so it can initialize the db, as long as you provide the same password for postgres when you re-install it.

Cannot install postgres with brew. Get "FATAL: role "postgres" does not exist" no matter what command I try

I've tried to install postgres 13.1 on my Mac using homebrew. (Also tried versions 11 & 12 which at one time were installed on this Mac, but encountering same error with those versions now). Am using notes I took from previous installs & of course google/stackoverflow. Tried many things/many times, but always, if I run psql, createuser, createdb (any postgres command), it responds with:
FATAL: role "postgres" does not exist
Also tried just creating a postgres user on my Mac to run install from that account; no luck.
Also tried postgresql install from the enterprisedb.com site. That worked, but seems klunky & seems to rely on .sh scripts. Interestingly, got the 'role postgres does not exist' error after the edb install too, but noticed if I just entered 'postgres' twice on the command line it worked. Example:
/Library/PostgreSQL/13/bin/psql -h localhost -p 5432 -U postgres postgres
This trick doesn't work tho with the brew install.
Understand I need to create the postgres role, but I can't find a command to do this that doesn't complain that the role doesn't already exist.
Any help would be so so appreciated!
Most sites documenting postgres install with homebrew said to use commands like:
psql postgres
or
createuser postgres
Those didn't work for me, but this did:
psql -d postgres -U <myUserName>
Attaching a screenshot to try to summarize the install experience & what worked:
screenshot
Can you try:
/Library/PostgreSQL/13/bin/psql
and then:
CREATE USER postgres SUPERUSER;
?
This document solved it for me: https://enrq.me/dev/2021/01/13/fix-role-postgres-does-not-exist/
The gist: run createuser -s postgres -U <os-username>

Postgres database setup on Cloud9 asks for sudo password

So I'm using the Cloud9 ide to collaborate on a Node.js project. We are using Postgresql for the database, and the documentation for Postgres seems to make it quite simple. I can sudo service postgresql start without a problem but I can't sudo -u postgres psql without it asking for my password, which I don't know because Cloud9 set up the environment for me. Any fixes or suggestions?
Instead of running sudo -u postgres psql try running sudo sudo -u postgres psql and you'll get right in. The documentation previously noted this but was recently changed. It will soon be reverted.
By default, the UNIX account "postgres" is locked, which means it cannot be logged in using a password. See this answer on serverfault for complete explanation.
Also try sudo -s to open a shell as root and tweak your system.

unable to create user postgres: role "postgres" does not exists

i am on ubuntu 12.04 server and i am trying to install postgresql. As of now, i have successfully installed it but unable to configure it. I need to create a role to move ahead and i ran this command in terminal :
root#hostname: createuser -s -r postgres
and it said :
createuser: could not connect to database postgres: FATAL: role "root" does not exist
Fine, so i did :
su - postgres
and then tried again
postgres#hostname: createuser -s -r postgres
and i got the error
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
and i get the same error when i do
psql -d dbname
Its like a loop, i am unable to create a role postgres because a role postgres does not already exist.
How do i fix this ?
The postgres version seems to be 9.1.x and the ubuntu version is 12.10
Turns out i had installed postgres-xc and postgresql on my machine. I had to knock off postgres-xc completely. And it was a little difficult to do that because, there was always an error --purge remove postgres-xc and the uninstallation could not continue.
There seems to be some kind of a packaging bug. (details on launchpad).
Eventually, i ended up doing this to make it work.
After that i uninstalled postgresql and installed it back to make it work.
Read postgresql tutorial it doesn't matter if it's Ubuntu or other Linux.
EDIT
before creating role or anything else on fresh install you need to create database cluster: have you created it?
initdb -D /usr/local/pgsql/data
You need to be logged as user postgres on linux machine. Here is more info.

Postgres createuser wants a password (not my system password)

I installed the latest version of postgres on OS X Lion using homebrew. The path is correct:
$ which postgres
/usr/local/bin/postgres
$ which psql
/usr/local/bin/psql
and brew doctor is happy. However, when I try to create a user, this happens:
$ createuser myuser
Password:
createuser: could not connect to database postgres: FATAL: password authentication failed for user <$USER>
The password is definitely not my regular user password and I have no idea how to find out what it's supposed to be. It's possible that I used homebrew to install a different version of postgres some time ago while installing a bunch of other stuff, but I don't recall. I did notice a different version of 'postgresql' that was installed alongside the new version of 'postgres', which I removed.
Don't know if this is a clue, but when I do this:
$ sudo su - postgres
The path to psql (but not postgres) is no longer correct:
postgres$ which postgres
/usr/local/bin/postgres
postgres$ which psql
/usr/bin/psql
Might be something simple. Would appreciate any help. Thanks!
Mac OS X Lion 10.7.5
homebrew 0.9.3
postgres 9.2.1
I usually do it like this:
createuser -P -s -e myuser
http://www.postgresql.org/docs/8.4/static/app-createuser.html
I solved the problem: I had to stop the server for the old version of postgres that came with my system. I didn't realize it, but I guess it starts on system start. Now I'm not having a problem.