PSQL does not detect .pgpass file - postgresql

I have tried
Setting the permission of .pgpass to 0600
Confirmed .pgpass is in my user's home directory (/home/brandon/.pgpass)
use -w in pg_dumpall command
Confirmed the data in .pgpass is correct
The script (https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux) for automated backup works if I type the password again and again which is why i added -w to all the commands. It for some reason does not read the .pgpass file .
There are multiple error messages but they are generally this.
pg_dumpall: could not connect to database "template1": FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
pg_pass file :
localhost:5432:my_db:postgres:password
command line :
username#droplet:~/ProjectDir$ bash cronjob/db_backup/pg_backup.sh
Making backup directory in /home/backups/database/postgresql/2017-12-06/
Performing globals backup
--------------------------------------------
Globals backup
cronjob/db_backup/pg_backup.sh: line 80: /home/backups/database/postgresql/2017-12-06/globals.sql.gz.in_progress: Permission denied
pg_dumpall: could not connect to database "template1": fe_sendauth: no password supplied
[!!ERROR!!] Failed to produce globals backup
Performing schema-only backups
--------------------------------------------
psql: fe_sendauth: no password supplied
The following databases were matched for schema-only backup:
Performing full backups
--------------------------------------------
psql: fe_sendauth: no password supplied
All database backups complete!

You should supply the actual calls to pg_dump and pg_dumpall for a more detailed answer.
However, from your output I see that pg_dumpall (?) tries to connect to database template1, but in your .pgpass file the only database is my_db.
You'll have to add entries to .pgpass for all databases to which you are trying to connect.
localhost:5432:mydb1:postgres:password
localhost:5432:mydb2:postgres:password
localhost:5432:mydb3:postgres:password
Alternatively, you are also able to put an * as a placeholder for different database names.
localhost:5432:*:postgres:password
Make sure that all the fields in .pgpass are the right/matching ones. If any of them (host, port, etc.) don't match, the unclear, uninformative and arguably wrong fe_sendauth: no password supplied error message will be the same.

Related

How to use dblink with out password in Postgres

My main concern is having passwords in the script (Postgres function), I am looking for a solution that how can avoid using passwords in Postgres functions.
I have a problem with using dblink_connect() in my function.
When I provide the password in the connection string, there is no problem, but when I don’t, it says:
ERROR: could not establish connection
DETAIL: fe_sendauth: no password supplied
However the .pgpass file in the path /var/lib/postgres/.pgpass does contain the proper password.
I have tried to connect to another database in the same server using dblink with psql from bash with the following command:
psql –h localhost –U myuser –w remote_db_name
It works fine.
My .pgpass file does have the line:
Localhost:5432:*:myuser:mypassword
It seems the psql finds the proper password in the .pgpass file but dblink doesn’t.

Changing password of postgres user led imidiate prompt of wrong password without providing a password

So I forgot the password of the postgres user on my PostgreSQL server. I then changed all the md5 settings in the pg_hba.conf file to trust, restarted the server and then changed the password of the postgres user using ALTER USER postgres.... Now I changed the trust settings back to md5, restarted the server again, but when I now want to perform psql -U postgres it immediately returns an error:
psql: FATAL: password authentication failed for user "postgres"
password retrieved from file "/root/.pgpass"
without asking me to enter any password. The .pgpass file has the following line in it:
*:*:*:postgres:SOME_HASH
EDIT:
Somehow this only appears when logging in as root to my server... With my other user it works just fine...
The immediate solution is to remove the .pgpass file that contains the wrong password. If you didn't create that file yourself, odds are that pgAdmin did that for you when you told it to save the password.
If you specify '-W', then psql will prompt you for a password and ignore the .pgpass file. But why not fix .pgpass, or if you aren't using it, then remove it?

Export everything from PostgreSQL

I want to export everything(users, roles, object definitions, data) from my PostgreSQL and restore it into another server. How can I do that?
I try to use
pg_dumpall -U postgres -g > out.sql
but file out.sql is empty. Can someone explain me why this happens?
When I try:
pg_dumpall -U postgres > out.sql
PostgreSQL just didn't want to accept password for "postgres".
I prefer to do this job from (windows) command line(it is not problem and with linux command, I can translate them). And if there is nice pgAdmin way to do it, I will happy to learn it, too.
P.S: My password for "postgres" is correct (I try to login with "psql -U postgres" and there is no problem)
Update: I try with user different than "postgres"(in this case rsmn):
pg_dumpall -U rsmn > out.sql
I have following error:
pg_dumpall: query failed: ERROR: permission dneied for relation
pg_authid pg_dumpall: query was: SELECT oid, rolname, rolsuper,
rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolconnlimit,
rolpassword, rolvalidunil, rolreplication, rolbypassrls,
pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, rolname
= current_user as is_current_user FROM pg_authid WHERE rolname !~ '^pg_' ORDER BY 2
User privileges(This user is only one beside "postgres"):
The attempt with user rsmn fails because it is not a superuser and cannot export the user passwords.
It remains to figure out why pg_dumpall -U postgres fails for you.
For that, the first thing would be to check the PostgreSQL server log. I am not sure where that is on your system, you should check the settings in postgresql.conf (logging_collector, log_destination, log_directory, log_filename). Probably it is in the Windows Event Log.
The log message will tell you what is wrong.
It could well be a problem with the pg_hba.conf file – if so, modern PostgreSQL versions will tell you which line was used.
For debugging purposes, you can try to change the relevant line to trust authentication so that no password is requested (but don't leave it that way, or everybody can connect as user postgres without password!).
If you are trying to do it under the *nix and your server is installed localy, you may not to use ident mode instead of password:
sudo su postgres
pg_dumpall > out.sql

.pgpass with AWS RDS

I need to run multiple commands on an AWS Postgres RDS instance I have. I don't want to enter the password each time. I'm trying to use the .pgpass file but I'm running into errors. The first time I ran into an error which said 'role "ubuntu username" does not exist'. I logged in as the postgres user and created that username. After this, the error I get said database does not exist. I have a feeling these errors have nothing to do with trying to connect to the AWS RDS instance.
psql --host=<awshost> --port=5432 --username=<awsrdsusername> --password --dbname=<dbname asks for a password and then logs me in after I enter it. Now I put a file in /home/<ubuntuusername> called .pgpass which has <awshost>:5432:<dbname>:<awsrdsusername>:<password>. Permissions for this file are set to 0600. Now when I run psql from the terminal and that produces the error - psql: FATAL: role "<ubuntuusername>" does not exist. These steps are as outlined on this page.
Can someone help me with the steps to get a pgpass file to connect to an AWS RDS instance?
.pgpass doesn't provide connection information. You seem to expect that after you create a .pgpass file, you can run psql without arguments and it'll know where to connect. That is not the case.
The hostname, port, etc you put in .pgpass are there so that PostgreSQL knows which line to look at when matching the connection info to find its password.
If you had to run:
psql -h something
to connect without a .pgpass file, you still have to run the same thing to connect with a .pgpass file.
If you run psql without arguments it'll connect to the local PostgreSQL (if any), using the current unix username as the postgresql username and the database to connect to. That's why you get the error you do.
If you want to change the default connection, you can use environment variables like PGHOST, PGPORT, etc, and/or a .pgservice.conf file.
See the manual to learn more.

How to provide password in one script to connect to postgresql database

I want to backup my postgresql database with a automatic backup program and that program only support a script line that backup with pg_dump.
I can't provide password in this script so I changed method from pg_hba.conf file from md5 to trust so that it won't ask for password and I used --no-password option.
How can I change this to md5 and provide password for it.
I would use a .pgpass file or use the peer authentication method. Details here:
Run batch file with psql command without password