pg_dump: How to set username and password for the *.sql output file? - postgresql

I want to create a local copy of an external postgres database. I am asked for the password of the local copy. Where can I set/access this password? I never set a password.
pg_dump -h external_hostname -p 5432 -U postgres db > db_copy.sql
createdb -p 5432 -h 127.0.0.1 -U postgres db_copy
I get the error
createdb: could not connect to database template1: FATAL: password authentication failed for user “postgres”

First you should find your pg_hba.conf file with
show hba_file;
Its place can be change but if you use debian based system, must be about etc/postgresql/9.6/main/pg_hba.conf
open this file and change
local all postgres peer
to
local all postgres trust
After all you should reload postgresql service from root, or easy way to reload is that, you run this select query by open psql :
select pg_reload_conf();

Related

psql: FATAL: password authentication failed for user "postgres" postgresql version 10

I recently installed Postgre version 10 on a linux red hat. I'm trying to configure that psql will prompt a user for password when accessing the database. After changing everything to scram-sha-256. I'm getting this error when accessing psql
psql: FATAL: password authentication failed for user "postgres"
This is my pg_hba.conf:
Any idea how can I fix this? Thanks!
Postgres store password md5 format as default. If you want to change password encryption you have to follow bellow solution:
P.S: Before start, You have to undo pg_hba.conf file authenticate method to md5
Edit postgresql.conf and change password_encryption to
password_encryption = scram-sha-256
Restart Postgres service (or reload service)
reset the user password
# if use psql cli
\password <user>
# If use SQL command
alter user <user> with password '<password>';
After updating all passwords you should change pg_hba.conf authenticate method to scram-sha-256 and reset service again
Reference: Information about upgrade postgres password authenicate
I know that something that solved to me was in setting a new password (in CMD) when it was necessary to give a password for psql to be recognized:
set PGPASSWORD= #type here the password you want to set
After setting, you write the following (for example):
psql -h 188.81.81.92 -U postgres -d postgres -p 5432
where -h stands for host, -U for user, -d for the database root, -p for port, using the postgreSQL language.
In windows, it could happen to outcome a problem: error: connection to server (...), which can be solved by typing:
C:\\Program Files\\PostgreSQL\\14\\bin\\psql.exe
this is, you open the file psql.exe and it will work fine.

How to connect to an alternative local postgresql cluster for the fist time?

In Ubuntu 16.04 I created second postgres database cluster, called cmg, with a local user as the admin user:
pg_create -u "local_username" -g "local_usergroup" -d /path/to/data/dir 9.5 cmg
The cluster was started with:
pg_ctrlcluster 9.5 cmg start
which ran successfully (pg_lsclusters show both are online)
The problem is I cannot connect to the cluster using psql as is normally done.
I tried using:
psql -h 127.0.0.1 -w -p5433 -U local_username
which fails with:
psql: fe_sendauth: no password supplied"
Is there any way to connect to the specific cluster?
use psql -h your_socket_dir -p5433 -U postgres to connect locally (uses peer auth by default - thus high chahce to login wothout password)
once logged in - set up password (create user if needed) and use it connecting remotely
psql -h 127.0.0.1 -p5433 -U local_username
in your connect string you had -w which is never ask for a password https://www.postgresql.org/docs/current/static/app-psql.html which would by default work only for local connections
I think the default pg_hba.conf when you start up a new cluster expects you to authenticate with peer connections, so you need to change user to your local user before connecting
[root#server~]# su - local_username
>> Enter password:
> password
[local_username#server~]# psql -h 127.0.0.1 -p 5433
You can check your pg_hba.conf file in /path/to/data/dir/pg_hba.conf to see how it expects you to authenticate.
Alternatively, if you cannot get access as your 'local_username' then instead su to postgres user in the instructions above and it should work

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

Postgres - How to use psql to create a database with the -c command and password authentication?

If I run the following:
psql -h localhost -U admin -c "CREATE DATABASE sales OWNER admin;"
It returns:
psql FATAL: database "admin" does not exist
I need to use password authentication and have setup .pgpass as follows:
localhost:*:*:admin:admin
Any ideas?
By default, when you connect as a user, the database connected to is the DB of the same name as the user.
If you want to connect to a different DB, you must specify it in the psql command line. Since in this case you're trying to create the DB, you can't connect to it yet, you must connect to another DB (like template1 or postgres) that already exists.
E.g.
psql -h localhost -U admin template1 -c "CREATE DATABASE sales OWNER admin;"
^^^^^^^^^

How to configure postgresql so it accepts login+password auth?

I have a fresh ubuntu 10.10 install with all updates and postgresql 8.4
In order for postgresql to accept login+password connections i have configured it via:
sudo su postgres
psql
ALTER USER postgres WITH PASSWORD 'password';
CREATE DATABASE myapp;
\q
exit
sudo vi /etc/postgresql/8.4/main/pg_hba.conf
change "local all all indent" to "local all all trust"
But, surprisingly, this is not working! The command
psql -U postgres password
Evaluates with error:
psql: FATAL: Ident authentication failed for user "postgres"
Any hints how i can make the psql -U to work?
It is probably a good idea to leave the "postgres" user with ident authentication. By default I believe Ubuntu uses the "postgres" user to perform upgrades, backups, etc, and that requires that it is able to login without a specified password.
I recommend creating another user (probably with your own username) and giving it admin privileges as well. Then you can use that user with passwords on local connections.
Here is what the relevant parts of my pg_hba.conf look like:
# allow postgres user to use "ident" authentication on Unix sockets
# (as per recent comments, omit "sameuser" if on postgres 8.4 or later)
local all postgres ident sameuser
# allow all other users to use "md5" authentication on Unix sockets
local all all md5
# for users connected via local IPv4 or IPv6 connections, always require md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Also note that psql -U postgres password will not do what you want. The password should never be specified on the commandline. That will try to login as user "postgres" to a database named "password".
You should use psql -U postgres myapp instead. Postgres will automatically prompt you for a password, if it is configured properly to require one.
In case we want the password be filled-in automatically, place it in $HOME/.pgpass file
I think your pg_ident.conf file is misconfigured. Also, have you tried
psql -U postgres -W
Another thing that can cause this is expired credentials. I don't think this happened in version 8, but in version 9 when you create a new role in pgadmin, it is created in an expired state and you need to change or clear the role's expiration date before you will be able to login with it.
You may find it helpful to create the database's user and schema in PostgreSQL:
Log into PostgreSQL from the postgres user
$ sudo -u postgres psql postgres
Once in, create the user and database
CREATE ROLE myuser LOGIN PASSWORD 'mypass';
CREATE DATABASE mydatabase WITH OWNER = myuser;
Log into PostgreSQL from the new user account
$ psql -h localhost -d mydatabase -U myuser -p <port>