Unable to authenticate in PostgreSQL - postgresql

I am using PostgreSQL 9.3 in an Ubuntu 14.04 and I always use this procedure to access psql:
sudo -i -u postgres
psql
Until now it returns this:
psql: FATAL: password authentication failed for user "postgres"
password retrieved from file "/var/lib/postgresql/.pgpass"
The content of /var/lib/postgresql/.pgpass is:
*:*:*:postgres:Vdjui6OTF6ab3Jzf77dVeOa88t7OdK
*:*:*:vmail:pxrw23PsaJOBDfZxIcImhAkzUzH4cA
*:*:*:vmailadmin:6XLhFMc5KWYpdXGE7TCfCHZSDkqzXJ
*:*:*:iredapd:5D6Yl2iRu56Dma2uRXDGSAP89JQQHE
*:*:*:iredadmin:xklX0IgJOQVGklDNFlLckPznq5L7ZG
*:*:*:sogo:PZdclHCua2Ip10yddVw4odSjr0dfZk
*:*:*:roundcube:kByuTSkilZWglYzv24unwfenUZkR7u
*:*:*:amavisd:UEBpiDEvsIbogEB5stJl1YP8DvMCWZ

I solved it! I entered this in the terminal instead:
psql -U postgres
Then input my password

Related

How do I solve this problem to use psql? | psql: error: FATAL: role "postgres" does not exist

I'm having trouble using PostgreSQL. I have recently installed this version (13+223.pgdg20.04+1) of postgresql package in ubuntu 20.04.
I'm trying to run psql command, but I get the following error:
psql: error: FATAL: role "my_username" does not exist
I have tried to create a new user with createuser me, but I get the following error:
createuser: error: could not connect to database template1: FATAL: role "my_username" does not exist
I have tried also forcing the postgres user with createuser me --username=postgres, but I get the following error:
createuser: error: could not connect to database template1: FATAL: Peer authentication failed for user "postgres"
How do I solve these problems to use PostgreSQL locally on my computer without these problems?
PD: I have reinstalled postgres and now I'm getting a different error while doing psql:
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I'm not sure why I had a bad installation, but I have completely uninstalled postgres following this post:
https://kb.objectrocket.com/postgresql/how-to-completely-uninstall-postgresql-757
after that I have restarted my computer and installed posgres again following the proper instructions in:
https://www.postgresql.org/download/linux/ubuntu/
and now it looks like it works without problems
Peer authentication means (there are advanced possibilities, but those are not going to be used by default, while the simple method is the default for apt-installed PostgreSQL) that you have to be the OS user 'postgres' to connect as the database user 'postgres'. So you would do:
sudo -u postgres createuser me
You don't need to specify --username=postgres, since that is the default behavior anyway once you use sudo -u postgres
Alternatively, you could change your pg_hba.conf to use a different authentication method other than peer, if you want to.
You need to provide username in the psql command using -U option.
psql -U postgres
Postgresql comes with a predefined superuser role called postgres. If you want to create more roles, you first have to connect as this initial role.
first check user postgres exists:
$ id postgres
Then:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.
If, Password for user postgres is no known then change it:
$ su - postgres
Password:
su: Authentication failure
$ sudo passwd postgres
New password:
Retype new password:
passwd: password updated successfully
Finally again:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.

Cannot access postgres PSQL

I have just installed postgres (mac os), however, when I go to access psql it tries to find the database not the user.
I know the username is benbagley (because that's my system name)
I have tried.
➜ ~ psql
psql: FATAL: database "benbagley" does not exist
and
➜ ~ psql -U benbagley
psql: FATAL: database "benbagley" does not exist
As stated in the manual psql assumes that database name is same as the username when the database is not provided explicitly and tries to connect with that database, which does not exist in your case.
Try connecting with default database which is postgres.
psql -U username -d postgres

Create local Postgresql account

On OS X 10.11.2, I've installed Postgres.app and I'm running the local server. I'm trying to create a local account with a username and password so that I can develop a Rails app locally. However, running the following command:
sudo -u postgres createuser -s {USERNAME}
I receive sudo: unknown user: postgres error.
Any suggestions as to why this error occurs and how to resolve this?
sudo tells you there is no system user "postgres".
When you installed PostgreSQL, it should have created database user "postgres" and you can try use that:
$ psql -u postgres
postgres=# create user {username} password '{password}';
I managed to create a user with the following steps.
In the terminal:
createuser --superuser {USERNAME}
I then set a password in psql:
\password {USERNAME}

role "postgres" does not exist; cannot createuser

I am on Linux Mint Cinnamon 2.2.16.
During the process of getting Rails up and running, I am having problems with Postgres.
postgres#BL ~ $ psql --version
psql (Postgres-XC) 1.1
(based on PostgreSQL) 9.2.4
I was unable to get anything working under my usual username, so I changed to the default user using
sudo su - postgres
I cannot get anything to work with createuser.
postgres#BL ~ $ psql
psql: FATAL: role "postgres" does not exist
postgres#BL ~ $ createuser -s -U $USER
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
postgres#BL ~ $ sudo -u postgres createuser newname
Sorry, user postgres is not allowed to execute '/usr/bin/createuser newname' as postgres on BL.
postgres#BL ~ $ which psql
/usr/bin/psql
postgres#BL ~ $ psql \l
psql: FATAL: role "postgres" does not exist
After thoroughly researching the problem and tearing out more than a few hairs, I decided this was some variation of a problem with packaging/installation, similar problem noted here: unable to create user postgres: role "postgres" does not exists
I did a complete uninstall, as per below, and reinstalled without postgresql-xc
How to thoroughly purge and reinstall postgresql on ubuntu?
The new install had expected behavior with the user "postgres" and I was able to add myself as a superuser and create new databases. After some post-installation finagling, Rails seems to be running and playing nice with postgres.
You have to authenticate to psql as the superuser in order to manage users.
For example
psql -U root
Afterwards
create user paige with password 'paige';

Setting up postgres on VPS for production

So, I have a Pyramid app with a postgres database on my local machine. I did a pg_dump to get a dump of the data in my database, called pg_dump_2014-04-22. I then git pushed this file, and did a git pull in the VPS to get the file.
Now, I have already installed postgres on my VPS. When I sudo -u postgres psql on my VPS, I can connect to it but there are no relations (naturally).
Both my username and database name are postgres.
So, I tried psql postgres < pg_dump_2014-04-22, but this gives the error psql: FATAL: role "root" does not exist.
I also tried pg_restore -h localhost -U postgres -d postgres pg_dump_2014-04-22, and that prompts me for my password, but then throws the error pg_restore: [archiver(db)] connection to database "postgres" falied: FATAL: password authentication failed for user postgres"
What am I missing here?
You first have to create a user and the database where you want to import your dump
su postgres
createuser root
createdb yourdb
Then import the dump with
psql -d yourdb -f pg_dump_2014-04-22