Ale*_* N. 283 postgresql bash command-line
我正在尝试使用shell脚本自动化数据库创建过程,并且我已经通过将密码传递给psql而遇到了一个问题.以下是shell脚本中的一些代码:
psql -U $DB_USER -h localhost -c"$DB_RECREATE_SQL"
Run Code Online (Sandbox Code Playgroud)
如何以非交互方式将密码传递给psql?
谢谢!
a_h*_*ame 517
在调用psql之前,在脚本中设置PGPASSWORD环境变量
PGPASSWORD=pass1234 psql -U MyUsername myDatabaseName
Run Code Online (Sandbox Code Playgroud)
供参考,请参阅http://www.postgresql.org/docs/current/static/libpq-envars.html
编辑
从Postgres 9.2开始,还可以选择指定可以包含用户名和密码的连接字符串或URI.
使用它是一种安全风险,因为在查看正在运行的进程的命令行时,密码是以纯文本形式显示的,例如使用ps
(Linux),ProcessExplorer(Windows)或其他用户的类似工具.
另请参阅有关数据库管理员的此问题
Fli*_*mzy 119
从官方文档:
拥有〜/ .pgpass文件也很方便,以避免经常输入密码.有关更多信息,请参见第30.13节.
...
此文件应包含以下格式的行:
hostname:port:database:username:password
Run Code Online (Sandbox Code Playgroud)
将使用第一行中与当前连接参数匹配的密码字段.
小智 97
在一行:
export PGPASSWORD='password'; psql -h 'server name' -U 'user name' -d 'base name' -c 'command'
Run Code Online (Sandbox Code Playgroud)
用命令 一个sql命令如"select * from schema.table"
或更具可读性:
export PGPASSWORD='password'
psql -h 'server name' -U 'user name' -d 'base name' \
-c 'command' (eg. "select * from schema.table")
Run Code Online (Sandbox Code Playgroud)Jac*_*din 48
我更倾向于将URL传递给psql:
psql "postgresql://$DB_USER:$DB_PWD@$DB_SERVER/$DB_NAME"
Run Code Online (Sandbox Code Playgroud)
这使我可以自由地命名我的环境变量,并避免创建不必要的文件.
这需要libpq
.文档可以在这里找到
ubi*_*ubi 24
使用PGPASSWORD
环境变量的另一种方法是conninfo
根据文档使用字符串:
指定连接参数的另一种方法是在 conninfo 字符串或 URI 中,它用于代替数据库名称。这种机制使您可以非常广泛地控制连接。
$ psql "host=<server> port=5432 dbname=<db> user=<user> password=<password>"
postgres=>
Run Code Online (Sandbox Code Playgroud)
小智 23
在Windows上:
为PGPASSWORD分配值: C:\>set PGPASSWORD=pass
运行命令: C:\>psql -d database -U user
准备
或者在一行中,
set PGPASSWORD=pass&& psql -d database -U user
Run Code Online (Sandbox Code Playgroud)
请注意&&之前缺少空格!
Pro*_*ton 22
只是为了增加更多清晰度。
您可以将密码分配给PGPASSWORD
变量。
因此,下面的内容将要求您输入密码:
psql --host=aurora-postgres.cluster-fgshdjdf.eu-west-1.rds.amazonaws.com --port=5432 --user=my_master_user --password --dbname=postgres
Run Code Online (Sandbox Code Playgroud)
我们将用 替换该--password
标志PGPASSWORD=QghyumjB3ZtCQkdf
。所以它将是:
PGPASSWORD=QghyumjB3ZtCQkdf psql --host=aurora-postgres.cluster-fgshdjdf.eu-west-1.rds.amazonaws.com --port=5432 --user=my_master_user --dbname=postgres
Run Code Online (Sandbox Code Playgroud)
这样您就不需要输入密码。
小智 20
这可以通过.pgpass
在(Linux)用户的主目录中创建文件来完成.
.pgpass
文件格式:
<databaseip>:<port>:<databasename>:<dbusername>:<password>
Run Code Online (Sandbox Code Playgroud)
您也可以使用外卡*
代替细节.
假设我想在tmp.sql
没有提示输入密码的情况下运行.
使用以下代码,您可以在*.sh文件中
echo "192.168.1.1:*:*:postgres:postgrespwd" > $HOME/.pgpass
echo "` chmod 0600 $HOME/.pgpass `"
echo " ` psql -h 192.168.1.1 -p 5432 -U postgres postgres -f tmp.sql `
Run Code Online (Sandbox Code Playgroud)
Raj*_*rma 12
如果在一个答案中添加大部分选项还为时不晚:
有几个选项:
设置一个环境变量并从那里获取它:
export PGPASSWORD='password'
然后运行你的 psql 登录,甚至从那里运行命令:
psql -h clustername -U username -d testdb
Run Code Online (Sandbox Code Playgroud)
在 Windows 上,您必须使用 "set" :
设置 PGPASSWORD=pass 然后登录到 psql bash。
将 pg_env.sh 的内容添加到我的 .bashrc 中:
cat /opt/PostgreSQL/10/pg_env.sh
#!/bin/sh
# The script sets environment variables helpful for PostgreSQL
export PATH=/opt/PostgreSQL/10/bin:$PATH
export PGDATA=/opt/PostgreSQL/10/data
export PGDATABASE=postgres
export PGUSER=postgres
export PGPORT=5433
export PGLOCALEDIR=/opt/PostgreSQL/10/share/locale
export MANPATH=$MANPATH:/opt/PostgreSQL/10/share/man
Run Code Online (Sandbox Code Playgroud)
添加(根据 user4653174 建议)
export PGPASSWORD='password'
Run Code Online (Sandbox Code Playgroud)
psql postgresql://myawsumuser:myPassword@127.0.0.1:5432/myawsumdb
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
241834 次 |
最近记录: |