如何从命令行执行PostgreSQL脚本文件,没有userinput / password [英] How to execute PostgreSQL script-file from command line without userinput / password

查看:252
本文介绍了如何从命令行执行PostgreSQL脚本文件,没有userinput / password的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在安装我的应用程序期间,我想创建一个PostgreSQL数据库和一些表和函数。

During the installation of my app, I want to create a PostgreSQL-Database and some tables and functions.

为此目的,我使用PSQL.EXE PostgreSQL。我有2个脚本。第一个创建数据库和具有在该数据库上执行脚本的权限的相应用户。我想要执行第二个脚本,因为这是刚刚创建的用户。不幸的是,我找不到一个方法来传递该用户的密码作为命令行参数。忽略密码会导致停止执行并提示用户输入密码,我想避免 - 因为这是在我的应用安装期间执行的。

For that purpose I use PSQL.EXE that ships with PostgreSQL. I have 2 scripts. The first one creates the database and a corresponding user that has rights to execute scripts on that database. I want to execute the second script as this just created user. Unfortunately I can't find a way to pass the password for that user as a command line argument. Omitting the password leads to a stop of execution and a prompt for the user to enter the password, which I would like to avoid - since this is executed during installtion of my app.

是否有任何方法可以传递密码作为参数,还是有任何其他命令行工具,我可以使用?

Is there any way to pass the password as argument or is there any other command line tool I could use?

更进一步解释环境。我使用WiX 3.5设置为MSI-Builder。

To explain the environment a bit further. I use WiX 3.5 setup as a "MSI-Builder".

推荐答案

您可以使用pgpass文件作为dbenhur answerd,您可以在调用psql之前设置环境变量PGPASSWORD:

You can either use a pgpass file as dbenhur answerd, or you can set the environment variable PGPASSWORD before calling psql:

SET PGPASSWORD=my_very_secret_password
psql somedb someuser

手册中记录了所有支持的环境变量: http://www.postgresql.org/docs/current/static/libpq-envars.html

All supported environment variables are documented in the manual: http://www.postgresql.org/docs/current/static/libpq-envars.html

这篇关于如何从命令行执行PostgreSQL脚本文件,没有userinput / password的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆