Windows PSQL 命令行:有没有办法允许无密码登录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6216432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:25:37  来源:igfitidea点击:

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

windowspostgresql

提问by A Question Asker

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.

我的目标是能够发出命令而不必提示我输入密码。有什么办法可以从 Windows 命令行实现这一点吗?在 Linux 中,我觉得我可以将密码发送到标准输入或其他东西,但我不确定是否可以为 Windows 执行此操作。

Thanks!

谢谢!

回答by Grzegorz Szpetkowski

There are two ways:

有两种方式:

  • Set environment variable PGPASSWORD e.g. set PGPASSWORD=yoursecretpassword
  • Use password file %APPDATA%\postgresql\pgpass.confas described in documentation
  • 设置环境变量 PGPASSWORD 例如 set PGPASSWORD=yoursecretpassword
  • 使用密码文件%APPDATA%\postgresql\pgpass.conf中所描述的文档

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:

在密码文件(我的位置是 C:\Users\Grzesiek\AppData\Roaming\postgresql\pgpass.conf)中使用以 doc 格式指定。例如,在本地 5432 服务器上连接数据库 postgres 作为角色 postgres 添加:

localhost:5432:postgres:postgres:12345

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

我检查了这个,它运行良好(对我来说),但不要使用 127.0.0.1)。

回答by leonbloy

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).

另一个方便的选择(特别是如果您的 PG 服务器在您自己的客户端机器上运行,并且如果这不会给您带来任何安全问题)是允许在服务器中无需密码登录(“信任”身份验证模式)。

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.

例如,pg_hba.conf(在您的 DATA 目录中,一个典型位置:)中的这一行C:\Program Files\PostgreSQL\9.0\data\允许从您的本地计算机无需密码即可访问。

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]

回答by klmbear

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 PHPASSWORDexecute copy command to a csv file and then clear environment variable, then go back to root directory.

我知道这是一个老问题,但对于像我这样的人来说,很难找到适用于 Windows 的解决方案。把它放在一个 .bat 文件中,它会起作用(至少对我来说是这样)。将director改为postres目录,设置环境变量PHPASSWORDexecute copy命令到一个csv文件,然后清除环境变量,然后回到根目录。

cd C:\Program Files\PostgreSQL.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:\

回答by Bill_Stewart

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.

我意识到这个问题现在有点老了,但我相信有一种更好的方法可以在 Windows 上进行安全、无密码的 PostgreSQL 登录 - 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 usernamewith the Windows user name (this is the sAMAccountNameattribute for domain accounts), and replace AUTHORITYwith 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 loginnamewith the PostgreSQL login name (to reduce confusion, I would recommend using the same name for both usernameand 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
    
  1. 使用相同的名称创建本地或域用户帐户和 PostgreSQL 登录。

  2. 在 中pg_ident.conf,创建一个映射:

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

    username替换为 Windows 用户名(这是sAMAccountName域帐户的属性),并将AUTHORITY替换为计算机名或短域名。如果您不确定AUTHORITY使用什么,请检查 PostgreSQL 日志文件。对于本地帐户,这将是计算机名称;对于域帐户,它可能是短域名。最后,将loginname替换为 PostgreSQL 登录名(为了减少混淆,我建议对usernameloginname使用相同的名称)。

  3. pg_hba.conf,允许登录;例如:

    # 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. 如果是域帐户,则为其设置一个 SPN;例如:

    setspn -S POSTGRES/serverfqdn username
    

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

现在您可以使用指定的用户名登录 Windows 并运行psql.exe等,无需密码。

回答by JAGJ jdfoxito

Steps:

脚步:

First ENviroment Var PGPASSWORD

第一个环境变量 PGPASSWORD

C:\Windows\system32>set PGPASSWORD=clave

C:\Windows\system32>set PGPASSWORD=clave

before

psql -d basededatos -U usuario

psql -d basededatos -U usuario

Ready,

准备好,

回答by Yvens Rebou?as Serpa

I found another useful solution that worked for me on this link. It basically sets the PGPASSWORDat the beginning of your command like this:

我在此链接上找到了另一个对我有用的解决方案。它基本上PGPASSWORD像这样在命令的开头设置:

PGPASSWORD=PASSWORD psql YOUR_CODE