Windows – PostgreSQL on Windows: psql expects me to log in with the Windows account

postgresqlwindows 7

I've just installed PostgreSQL 9.6 on Windows 7. I had to choose a password for the default user "postgres" during the install process.

Running pgAdmin4 after the install, I typed in my password for "postgres" and it was saved as expected.

But with the command line, the default user doesn't seem to be "postgres" but my Windows session user name, for which I don't have a PostgreSQL password (my Windows password doesn't work there). I can login with psql -U postgres, but how can I change the default user to be postgres?

Best Answer

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

or set it permanently as mentioned in other questions such as Change environment variables as standard user.

In fact there are quite a few other variables corresponding to other connection properties that can be set similarly (see Environment Variables in the PostgreSQL documentation for the full list).

These variables are a feature of libpq, the DLL implementing the client-server interaction for most PostgreSQL applications, so they will work not just for psql but for all these applications.

Related Question