命令行如何帶上密碼?
可以參考stackoverflow里這個回答:
postgresql-scripting-psql-execution-with-password
里面寫道:
"The obvious way is via the password prompt
Instead of that, you can...
provide the password in a pgpass file or through the PGPASSWORD environment variable."
最顯而易見的方法是通過控制台提醒你來輸入密碼,
除此之外你還可以:
- 把密碼放在pgpass文件
- 把密碼放在PGPASSWORD環境變量里
具體怎么配置pgpass文件和PGPASSWORD環境變量可以參考下面兩篇官方文檔。
- https://www.postgresql.org/docs/9.0/static/libpq-pgpass.html
- https://www.postgresql.org/docs/9.0/interactive/libpq-envars.html
使用PGPASSWORD環境變量
Command-line There is no option to provide the password as a command line argument because that information is often available to all users, and therefore insecure. However, in Linux/Unix environments you can provide an environment variable for a single command like this:
PGPASSWORD=yourpass psql ...psql -h 192.168.1.8 -p 5432 -U lg -d mydb -c "select * from myschema.table"
命令行:psql命令行沒有提供密碼參數,因為這樣的話密碼很可能會被所有用戶知道,這樣不安全。但是在Linux/Unix環境中你可以像下面這個命令一樣,搞個環境變量即可:
PGPASSWORD=yourpass psql -h 192.168.1.8 -p 5432 -U lg -d mydb -c "select * from myschema.table"
