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

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.

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 to change default username of postgres (not rename username postgres to something) in windows

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

PSQL not prompting for password in PowerShell

This PSQL statement in PowerShell:
.\psql --% -h localhost -p 5000 -U postgres -d mydatabase -c `
(SELECT * ...... do something);
Works - and does not prompt for a password.
.\psql.exe --help
States the -w switch is required for no password.
Why does this command work without a password? The database has a password.
The command does not work in the PSQL interactive shell without password authentication, only in PowerShell.
-w is not required for "no password" reread what it says, from my man psql
-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. Note that this option will remain set for the entire session, and so it affects uses of the meta-command \connect as well as the initial connection attempt.
-w is meant for when you're running psql scripts and input for a password is not possible, like in a headless session.
Why does this command work without a password? The database has a password.
Even if the database has a password, if your pg_hba.conf file has trust no password will be required. To debug this further we need to see your pg_hba.conf.

Changing psql database name

When I run psql, the user it defaults to is my username:
$ psql
psql (9.4.5)
Type "help" for help.
jeffrey.wan=#
How do I change it to something else?
psql -U jeffrey.wan desired_user
psql (9.4.5)
Type "help" for help.
desired_user=#
Type \? to get the list of the available commands, and you will see that the one you are looking for is:
\c desired_user
By the way, the displayed name is the database you are connected to, not the user you are connected as.
You can specify another user by typing it as second parameter.
But you'd better look up for the command line parameters when you connect the first time. For example, this will bring you where you want directly:
psql -d database -U user -h hostname

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