Postgresql: cannot access psql after changing user password - macos

After a few weeks of not using my postgres db, I need to use it again. To my knowledge I haven't changed anything in the configuration since then, but I cannot login as my normal superuser.
psql -d %dbname% -U %username%
I get: psql: FATAL: password authentication failed for user "%username%"
Here's what I've done so far:
Sudo into the db: sudo -u %OS_username% psql
Test create a user to verify commands work: CREATE USER test WITH PASSWORD 'test'; (it worked)
Change my password: ALTER USER %username% PASSWORD 'test'
Reboot the server: pg_ctl restart -D ~/Library/Application\ Support/Postgres/var-9.3
Then I try psql -d %dbname% -U %username% again, but it fails with the same message.
Below are the active connection configurations from my pg_hba.conf:
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
local all %username% trust
I have only added the last line today, and made no other changes. PG Commander/PGAdmin refused to login as normal (as they always have done). Why and where could the credentials have been changed?
Edit: I should also note that I cannot login with the test user I created.

Related

pg_restore ignores .pgpass and PGPASSWORD environment variable

I want to import a backup using pg_restore without a password prompt.
I tried several options but after I run the script it will always ask for a password. pg_dump is working but not pg_restore. I can run the pg_restore command if I enter my password but I want a passwordless command or at least I don't want to enter my password because the script has to work without user interaction.
What is working for me:
PGPASSWORD=xyz pg_dump -h localhost -U user -Fc database > ~/dump_prod.pgsql
What is NOT working
1.)
PGPASSWORD=xyz pg_restore -h localhost -d database -U user -W --clean --no-owner ~/dump_prod.pgsql
2.)
pg_restore --dbname=postgresql://user:pass#localhost:5432/db -W --clean --no-owner ~/dump_prod.pgsql
3.)
touch ~/.pgpass
echo "*:*:*:*:password > ~/.pgpass
chmod 0600 ~/.pgpass
pg_restore -h localhost -d db -U user -W --clean --no-owner ~/dump_prod.pgsql
any ideas?
Regards
As per the doc, -W will prompt for a password. -w will not
-w
--no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as
a .pgpass file, the connection attempt will fail. This option can be
useful in batch jobs and scripts where no user is present to enter a
password.
-W
--password
Force pg_restore to prompt for a password before connecting to a database.
The .pgpass file worked for me.
My setup:
I am restoring DB into the postgres instance running as Docker container.
The postgres instance is run using command:
docker run --name postgres_db -p 5432:5432 -e POSTGRES_PASSWORD=admin -d postgres:9.6
pg_restore is present but it is available as separate tool, outside docker setup.
The ~/.pgpass file entry looks like this:
localhost:5432:db_name:user:password
Where:
db_name is the target db name that one is going to restore.
user is the name of the user that is going to perform the restore - in my case an admin user. i.e. postgres user in the postgres instance.
password - admin user's password. i.e. admin

Running psql from shell always ask for connection details

I am trying to run psql command from windows command line. However, command always ask for connection details even though connection details are given.
I have tried below two commands but none works:
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat -f d:\test.sql postgresql://postgres:password#localhost:5432/testdb
and
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat" -h localhost -d testdb -U postgres -p 5432 -f d:\test.sql
I have created password file to store password as mentioned here.
However, in command line, it asks for host name, database and other details.

Role "postgres" doesn't exist on Windows 7

When running the following command after pg_ctl initdb and pg_ctl start commands:
C:\Program Files\PostgreSQL\11\bin>psql.exe -U postgres -h localhost
I am getting the following error:
role "postgres" doesn't exist
All similar questions are about Linux or Mac and they don't help me much, so I don't know how to fix it.
Since you didn't specify a username with -U when you ran initdb, the bootstrap superuser has the name of the operating system user with which you ran initdb.
So you can simply connect with
psql
If you would like to change the name of the user to postgres, that is simple:
-- get the name of the current user in a variable
SELECT current_user \gset
CREATE ROLE dummy SUPERUSER;
-- connect as user "dummy"
\c - dummy
-- rename the bootstrap user to "postgres"
ALTER ROLE :"current_user" RENAME TO postgres;
-- remove "dummy"
\c - postgres
DROP ROLE dummy;
-- disconnect
\q

cannot connect to template1: FATAL: role "_postgres" does not exist

Working on OS X 10.10, installed postgreSQL and PostGIS from
here, psql vers 9.3.5. I am having a hard time getting postgreSQL running.
I try to follow the OSM tutorial on psql and PostGis. But that seems rather hard... I am stuck here.
They suggest to create the user like:
sudo -u postgres createuser christoph
That did not work for me. I created the user like this:
$ sudo -u _postgres createuser -U postgres christoph
That worked without any problems. Then I tried this:
$ sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
> cannot connect to template1: FATAL: role "_postgres" does not exist
How can _postgres not exist if I just created a role with it?!
I also do not understand why
$ createdb mydb
> -bash: createdb: command not found
does not work although
I added the path like:
$ export PSQL_DIR=/usr/local/pgsql/bin/
$ export PATH="$PSQL_DIR:$PATH"
before.
Executing the command like:
$ /usr/local/pgsql/bin/createdb mydb
> createdb: cannot create database: ERROR: permission denied to create database
I am looged in with the user who installed psql...
Is that connected?!
It is very easy to confuse system users (stored in /etc/passwd) with Postgres roles (stored inside the database cluster).
In your first command, you mention both types of user:
sudo -u _postgres means "use the system user _postgres to execute the following command"
the -U postgres in the command means "connect to the Postgres cluster with the role postgres"
However, in the next command:
sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
You specify the same sudo -u _postgres, but are missing the -U argument to the actual Postgres command (you say you want christoph to own the new DB, but you don't say "I am christoph").
The Postgres command-line tools have a default behaviour of guessing that the Postgres role name is the same as the system user name. Since the system user is named _postgres (with leading underscore) and the Postgres role is named postgres (no leading underscore), this assumption fails, and you can't connect.

mysqldump cannot connect using socket

This issue has been racking my brain for a few hours. I have been trying to use mysqldump to dump a database, using:
mysqldump --protocol=socket -S /var/run/mysqld/mysqld.sock database`
However, I keep getting:
1045: Access denied for user 'root'#'localhost' (using password: NO) when trying to connect
I am on localhost and running under root (sudo su).
Root#localhost is allowed in the mysql user table.
I can use > mysql to view all of the databases, but mysqldump will not work.
I do not know the root password (system generated).
I have tried adding the socket to the my.conf like so and restarting the mysql server:
[mysqldump]
socket = /var/run/mysqld/mysqld.sock
Any help would be appreciated!
Even though you are connecting via the socket, you must still give the user root
If root#localhost has no password then do this:
mysqldump -uroot --protocol=socket -S /var/run/mysqld/mysqld.sock database
If root#localhost has a password then do this:
mysqldump -uroot -p --protocol=socket -S /var/run/mysqld/mysqld.sock database
If running
mysql
lets you login with specifying -uroot, try not specifying the socket either
mysqldump database
I just noticed that the socket you specified for mysqldump is
[mysqldump]
socket = /var/run/mysqld/mysqld.sock
You need to make sure the socket is defined under the [mysqld] section of my.cnf as well
If this does not exist
[mysqld]
socket = /var/run/mysqld/mysqld.sock
then run this query
SHOW VARIABLES LIKE 'socket';
and make sure of the socket file's name and path.
You could have you System DBA add a custom user for you
GRANT ALL PRIVILEGES ON *.* TO tyler#localhost;
Then, you can run
mysqldump -utyler --protocol=socket -S /var/run/mysqld/mysqld.sock database
This is not secure. tyler should have a password. So, run this:
SET SQL_LOG_BIN=0;
GRANT ALL PRIVILEGES ON *.* TO tyler#localhost IDENTIFIED BY 'tylerspasssword';
then you can do
mysqldump -utyler -p --protocol=socket -S /var/run/mysqld/mysqld.sock database
Give it a Try !!!
I found the solution! The socket does not hold the credentials itself. They are stored in the /root/.my.cnf configuration file instead. Mine only had the username and password for the mysql command. I needed to add [mysqldump] to it as well. Here is what my /root/.my.cnf file looks like now:
[mysql]
user=root
pass=myawesomepass
[mysqldump]
user=root
pass=myawesomepass

Resources