谢谢!
有两种方法:
set PGPASSWORD=yoursecretpassword
%APPDATA%\postgresql\pgpass.conf
在密码文件(我的位置为 C:\Users\Grzesiek\AppData\Roaming\postgresql\pgpass.conf)中使用文档中指定的格式。例如,要在本地 5432 服务器上将数据库 postgres 作为角色 postgres 连接,请添加:
localhost:5432:postgres:postgres:12345
我经过测试这个方法可以很好地运行(对我来说),但不要使用127.0.0.1。
pg_hba.conf
文件中的此行(在您的 DATA 目录中,典型位置为:C:\Program Files\PostgreSQL\9.0\data\
)允许从您的本地机器无需密码访问。host all all 127.0.0.1/32 trust
然后,连接
psql.exe -h 127.0.0.1 -U postgres -w [YOUR_DB_NAME]
我知道这是一个老问题,但对于像我一样正在寻找答案的人来说,在Windows上很难找到解决方案。将以下内容放入.bat文件中,它将起作用(至少对我来说是这样)。更改目录到postres目录,设置环境变量PGPASSWORD
,执行复制命令到csv文件,然后清除环境变量,最后返回根目录。
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:\
我知道这个问题现在有点老了,但我相信有更好的方法可以在Windows上实现安全、无需密码的PostgreSQL登录-SSPI。
Create a local or domain user account and PostgreSQL login with the same name.
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).
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
If a domain account, set a SPN for it; e.g.:
setspn -S POSTGRES/serverfqdn username
psql.exe
等操作而无需密码。对于那些不想要 .pgpass 文件并希望在单个命令行实例中执行的人来说,这是另一种选择。
set "PGPASSWORD=yourpass" && createdb -h localhost -p 5432 -U postgres new_db_name
我参考了几乎所有与命令行相关的解决方案,最终在设置Windows 11中PGPASSWORD=yourpass时使用了"
。
$Env:PGPASSWORD='your-pass'; psql -U postgres
我在Postgres 15中使用以下方法使其工作:
psql -U postgres -h 127.0.0.1 -p 54322 -f some_file password=postgres
J
我发现在这个链接里有另一个对我很有用的解决方案。它基本上会在你的命令开头设置PGPASSWORD
,像这样:
PGPASSWORD=PASSWORD psql YOUR_CODE