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

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.

Related

Forgot Password for PostgreSQL 13 db on Mac

I am new to PostgreSQL and before I start learning got stuck in the password;
I have forgotten my password to PostgresSQL 13 path when I use pdAdmin tool.
I use mac. Apologies for basic questions.
I went through multiple articles, videos but I am unable to through different steps.
Steps I did:
Downloaded PostgreSQL 13, logged into pgAdmin, forgot password to PostgreSQL 13 db
Found the "pg_hba.conf" and edited the method from "md5" to "trust"
Steps I am unable to proceed and stuck:
Restart the postgresql service (where and how do I do this? via launching Terminal?)
Start psql session as postgres (how to launch? I searched the psql via search. When I entered "psql -U postgres", its asking for "Database" and "Port (5432)" and "Username". I am unaware to find these"
This is where I am unable to go to next step to use ALTER USER command to reset.
I went through this but when I enter the command "sudo -u user_name psql db_name", its asking for all information above which I don't have knowledge yet. Same as above points where I am getting stuck.
Many thanks in advance.
If you installed Postgres using brew (That's most common case)..
To access postgres
# psql postgres
postgres is the database name, and after this you will be logged into psql and will see postgres db. You can switch to your database as -
\c <your-db-name-here>
To start/stop/restart
brew services start postgresql
brew services stop postgresql
brew services restart postgresql
If you did not install using brew and want to do so -
brew install postgres

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>

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

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

PostgreSQL installation done but commands are no working

I have installed PostgreSQL on my CentOS 6.5
I have installed the same with
http://www.enterprisedb.com/products-services-training/pgdownload
Now Postgres is running on 5432 that I have checked with
netstat -ant|grep 5432
but when I run a command like postgres or psql
I am getting error that
command not found
Please find attached image of the same.
Are you sure you want to be doing this as the root user and not as the postgres user?
If you login as the postgres user you'll have the right path and "psql" will connect as the postgres user.
Alternatively, you could say "psql -U postgres" and then when you're prompted for a password, the one that you selected at install time should work.

Postgres: Permission denied for all but phpPgAdmin

Just installed OS X Server for Lion. I'd heard that the default database is now Postres - which is good news. Bad news - I can't connect to it.
I've tried using psql, createdb and Navicat, and all return the same thing: "Could not connect to server: Permission denied." This is using the _postgres role and using my own role which I've added as a superuser to postgres.
The weird thing is, phpPgAdmin has no trouble at all in connecting. Neither do I when I'm running as root and use psql -U. But if I just straight up pqsl postgres or createdb whatever then it flat-out refuses to work.
Here's a summary:
gormster$ psql postgres # permission denied
gormster$ createdb whatever # permission denied
gormster$ sudo psql postgres # role "root" does not exist
gormster$ sudo su -
root# psql postgres # role "root" does not exist
root# psql -Ugormster postgres # THIS WORKS
What is going on?
Nevermind, it magically started working again. Not sure what it is I did to get it to happen, but it may have been adding myself to the _postgres group. If you're unaware, there are instructions for adding yourself to a group here.