Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows PSQL command line: is there a way to allow for passwordless login?

My goal is to be able to fire off a command without having to be prompted for my password. Is there any way to achieve this from the windows command line? In Linux I feel I could send the password to standard in or something, but I am not sure if I could do this for windows.

Thanks!

like image 454
A Question Asker Avatar asked Jun 02 '11 15:06

A Question Asker


People also ask

How do I log into Postgres without password?

Edit the pg_dba. conf file and change all local connections from md5 to trust. By doing this, you can log in to the PostgreSQL database server without using a password.

What are psql meta commands?

Meta-Commands. Anything you enter in psql that begins with an unquoted backslash is a psql meta-command that is processed by psql itself. These commands make psql more useful for administration or scripting. Meta-commands are often called slash or backslash commands.

What does psql command do?

psql is a terminal-based front-end to PostgreSQL. It enables you to type in queries interactively, issue them to PostgreSQL, and see the query results. Alternatively, input can be from a file or from command line arguments.


4 Answers

There are two ways:

  • Set environment variable PGPASSWORD e.g. set PGPASSWORD=yoursecretpassword
  • Use password file %APPDATA%\postgresql\pgpass.conf as described in documentation

Within password file (my location is C:\Users\Grzesiek\AppData\Roaming\postgresql\pgpass.conf) use specified in doc format. For example to connect database postgres as role postgres on local 5432 server add:

localhost:5432:postgres:postgres:12345

I checked this and it works well (for me), but don't use 127.0.0.1).

like image 94
Grzegorz Szpetkowski Avatar answered Oct 09 '22 15:10

Grzegorz Szpetkowski


Another handy option (specially if your PG server runs in your own client machine, and if this does not poses any security problem for you) is to allow login without password in the server ("trust" authentication mode).

For example, this line in pg_hba.conf (in your DATA dir, a typical location: C:\Program Files\PostgreSQL\9.0\data\ ) grants access without password from your local machine.

host     all     all     127.0.0.1/32    trust

Then, connect with

  psql.exe -h 127.0.0.1 -U postgres -w [YOUR_DB_NAME]
like image 22
leonbloy Avatar answered Oct 09 '22 16:10

leonbloy


I know it is an old question but for anyone looking like I was, it is hard to find a solution for windows. stick this in a .bat file and it will work (at least for me it did). change director to postres directory, set environment variable PGPASSWORD execute copy command to a csv file and then clear environment variable, then go back to root directory.

cd C:\Program Files\PostgreSQL\9.5\bin\

set PGPASSWORD=yourpassword


psql -d databasename -U yourusername -w -c "\COPY (select * from yourtable) TO 'c:/Users/yourdirectory/yourcsvfilename.csv' DELIMITER '|' CSV HEADER;"

set PGPASSWORD=

cd c:\
like image 26
klmbear Avatar answered Oct 09 '22 16:10

klmbear


I realize this question is a bit old now, but I believe there is a better means for secure, password-free PostgreSQL logon on Windows - SSPI.

  1. Create a local or domain user account and PostgreSQL login with the same name.

  2. In pg_ident.conf, create a mapping:

    # MAPNAME   SYSTEM-USERNAME      PG-USERNAME
    SSPI        username@AUTHORITY   loginname
    

    Replace username with the Windows user name (this is the sAMAccountName attribute for domain accounts), and replace AUTHORITY with the computer name or short domain name. If you're not sure what to use for AUTHORITY, check the PostgreSQL log file. For a local account, this will be the computer name; for a domain account, it's probably the short domain name. Lastly, replace loginname with the PostgreSQL login name (to reduce confusion, I would recommend using the same name for both username and loginname).

  3. In pg_hba.conf, allow the logon; e.g.:

    # TYPE   DATABASE   USER        ADDRESS        METHOD
    host     all        loginname   127.0.0.1/32   sspi map=SSPI
    host     all        loginname   ::1/128        sspi map=SSPI
    
  4. If a domain account, set a SPN for it; e.g.:

    setspn -S POSTGRES/serverfqdn username
    

Now you can log onto Windows using the specified username and run psql.exe, etc. without needing a password.

like image 26
Bill_Stewart Avatar answered Oct 09 '22 14:10

Bill_Stewart