How to change default username of postgres (not rename username postgres to something) in windows - postgresql

When I login to postgres in Windows cmd using psql command it by default tries to login to the user name ADMIN which is my windows user account name, and such user name actually does not exist in the database, I know how to change that by -U attribute like this psql -U postgres. I want to login with user name postgres by default without specifying in the command prompt. like changing any configuration files etc. How do I do that ?

That thread suggests by setting an environment variable:
https://superuser.com/questions/1278748/postgresql-on-windows-psql-expects-me-to-log-in-with-my-windows-account
The PGUSER environment variable is considered when the -U option is
not set.
So you may use a batch file essentially doing:
set PGUSER=postgres
psql

Related

How to pass password inline initdb postgres?

I am using postgres 9.3 . I want to make a script to create my database cluster and supply the password inline in the terminal. I know you can do it from file, but is there a way to do it command line?
that is the line I am using right now : 'initdb -D path/to/cluster -W -A password'
it then prompt me for password, I tried to provide it inline, but it does not work. Any ideas?
thanks
You can accomplish this with a shell trick. Assuming bash shell:
initdb -D path/to/cluster -A password --pwfile=<(echo secretpassword)
(Although you should never use -A password, use at least md5.)
As for your comment, it is hard to say what is going on. You don't show us starting the server at all, or setting the port to start on to 5555, nor creating a user named 'dbuser'.
thanks everyone!
It worked when I changed 'password' to 'md5' in my initdb statement.
although in the password.txt file, I can only store the password. If I follow the documentation from postgres
host:port:username:password it does not work anymore
I solved the connection problem with .pgpass. I've created the .pgpass file in the home directory and I was able to connect using: "psql -U username -d database -pXXXX"
Ok, my initial question was to supply the password inline in the terminal. But ,the proper way of doing is using the password file for security reason. Second, in the initdb statement use at least md5 for encryption. The password will be for the superuser for the default database. You statement should look like this:
"initdb -D /path/to/dbCluster -A md5 --pwfile=/path/to/password.txt"
Now, if you want to automatically connect to the database with psql without password prompt, you have to create a .pgpass file (linux) or pgpass.conf file (windows) with your user and password info in this format: host:port:db_name:user_name:password
Where you put those file is important:
Windows : /Users/user_name/AppData/Roaming/postgresql/pgpass.conf
(If the postgresql folder does not exist, you have to create it)
Linux : /home/user/.pgpass (with chmod 0600 permission on the file)
How to force psql to detect .pgpass file on Windows 10 system?

How do I make postgres user as the main user instead of typing psql -U postgres?

I install my psql 12.4 using this: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
When the system prompt me to enter password for user "postgres". I did give it a password.
But when I type psql on powershell terminal, it will require me to give password for user JinTan which i do not have.
(base) PS C:\Users\JinTan> psql
Password for user JinTan:
The only password i have is this:
(base) PS C:\Users\JinTan> psql -U postgres
Password for user postgres:
psql (12.4)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=#
So my question is:
How do I make postgres user as the main user instead of typing psql -U postgres?
Thanks all for the advise. I have solved it using all the advise.
I use 'a_horse_with_no_name' method to add PGUSER with value of 'postgres' in my env variable.
To not allow system to ask for password everytime,
I use "Belayer" method to change all of the "md5" value in pg_hba.conf to "trust".
Then it works like charm without needing me to enter password everytime.

PostgreSQL define user parameter for command line psql.exe

How do I define the user for PostgreSQL on the command line?
If I want to sign in as root I would use the following with MySQL...
mysql -uroot -p
Also is there a way to list users? I'm connected via pgAdmin III so if a list is available there I just haven't found it.
You have to use the U parameter as following:
psql -U name_of_user
Source
Thanks to #a_horse_with_no_name, programs use /? to list parameters by default. The following is the default user without a password (even though the setup process asked for a password and it does not match for the default user on the command prompt but it does in phAdmin III).
psql --username=postgres

How to use PGPASSFILE environment variable?

I am using areca backup program and I wrote a script in it to backup postgre database.
I want to get password via pgpass.conf file but I can't give it's path to script.
How can I use PGPASSFILE ?
This is my script (; is separator) :
-U;postgres;-w;-F;custom;-b;-f;D:\satraAutoBackup\Daily\Saturday\postgresql\geoMolkBackup;geoMolkPortal2
Create .pgpass file with content
host:5432:somedb:someuser:somepass
set the permissions using command
sudo chmod 600 .pgpass
Set the file owner as the same user using which you logged in :
sudo chown login_username:login_username .pgpass
Set PGPASSFILE environment variable :
export PGPASSFILE='/home/user/.pgpass'
Now check by connecting to database (remove -w) :
psql -h host -U someuser somedb
It will not prompt for password and logged in to postgresql.
You can do that by setting the environment variable PGPASSFILE to the path of pgpass.conf and by removing the -w parameter.
eg.:export PGPASSFILE="/path/to/pgpass.conf"
PS: Make sure to set the pgpass.conf in a secure place and locking it using NTFS permission for example.
A use case here on this wiki page

How to execute PostgreSQL script-file from command line without userinput / password

During the installation of my app, I want to create a PostgreSQL-Database and some tables and functions.
For that purpose I use PSQL.EXE that ships with PostgreSQL. I have 2 scripts. The first one creates the database and a corresponding user that has rights to execute scripts on that database. I want to execute the second script as this just created user. Unfortunately I can't find a way to pass the password for that user as a command line argument. Omitting the password leads to a stop of execution and a prompt for the user to enter the password, which I would like to avoid - since this is executed during installtion of my app.
Is there any way to pass the password as argument or is there any other command line tool I could use?
To explain the environment a bit further. I use WiX 3.5 setup as a "MSI-Builder".
You can either use a pgpass file as dbenhur answerd, or you can set the environment variable PGPASSWORD before calling psql:
SET PGPASSWORD=my_very_secret_password
psql somedb someuser
All supported environment variables are documented in the manual: http://www.postgresql.org/docs/current/static/libpq-envars.html
You can't supply password via cmdline arg (and don't want to as that's poor security practice).
You can provide a .pgpass file to support automatic script authentication. Here's the docs.
Better still, if you have access to create the db role then you already have all the access you need without having to carefully log in with a password. Have the second script operate under the same user as the first but include the following line to switch user:
set role my_new_user;
Where my_new_user is the name of the role you want to run it as.
If you only divided the scripts because of the different logins then with this they can go in the same file and just switch role mid way through.
Note:
On the off chance that you are not creating the DB and new role as a super user this may be a little more complex. If this is the case you will need to create the new role with:
create role my_new_role ... ADMIN my_role;
Where my_new_role is the role you're creating and my_role is your current user. Then when you're finished simply:
revoke my_new_role from my_role;
For completion, you can also use URI (doc link)
List dbs
psql "postgresql://username:password#localhost/postgres" -l
I also crafted this command to have only names (please tell me if you know a better way):
psql "postgresql://username:password#localhost/postgres" -l | awk -F '|' '{print $1}'| sed -e '/^\s*$/ d' -e '1,3d'|sed '$d'|awk '{print $1}'
You can also use unix socket to connect:
# ss -x -a |grep postgres|awk '{print $5}'
/var/run/postgresql/.s.PGSQL.5432
Note that the parent directory of the socket is used:
# sudo -u postgres psql -d "postgresql:///postgres?host=/var/run/postgresql/" -l
You can only do this if you have this line in your pg_hba.conf:
local all postgres ident
"ident" uses unix user for authent
dump a db
Here I added a different port number
pg_dump -Fc "postgresql://username:password#localhost:9001/${db}" > "backup_${db}.pgdump"
With dumpall you need a super user or role (with CREATE ROLE ... SUPERUSER). And it must have access to all DB. By default postgres can.
but in my case I couldn't use pg_dumpall with postgres because his password was removed by devs.
So I used:
sudo -u postgres pg_dumpall -d "postgresql:///?host=/var/run/postgresql/" > all.dump
tested version
# cat /opt/postgresql/PG_VERSION
9.6
hth