验证PostgreSQL用户和密码数据库无 [英] Authenticate PostgreSQL user and password without database
问题描述
我如何验证PostgreSQL用户和密码是否有效没有数据库的存在?
How do I verify the postgresql user and password are valid without the existence of a database?
我创建一个自动安装,这将创建一个数据库和运行SQL脚本来创建表。安装需要运行脚本之前验证用户名和密码。自动调用安装Windows批处理文件。首先,我设置密码,设置 = PGPASSWORD输入mypassword
。
I'm creating an automated installation that will create a database and run sql scripts to create tables. The install needs to authenticate the user and password before running the scripts. The automated install calls Windows batch files. First I set the password, set PGPASSWORD=mypassword
.
设置密码后,有什么命令可以验证用户名和密码,并返回一个错误code或消息?
After setting the password, what command can authenticate the user and password and return an error code or message?
其他数据库程序(如IBM和DB2)有一个附加命令,它可以让用户连接到服务器或实例,而无需指定数据库名。我找不到PostgreSQL的等价物。
Other database programs (like IBM and db2) have an attach command which lets the user attach to the server or instance, without specifying a db name. I cannot find the equivalent for PostgreSQL.
如何登录到PostgreSQL在命令行中不指定数据库名称?
How do I login to PostgreSQL on the command line without specifying a database name?
推荐答案
使用系统表pg_roles
的Postgres始终安装一个名为Postgres的数据库。 Postgres的是,当你没有连接到数据库连接数据库。在有一个叫表 pg_roles
。
Postgres always installs a database called "postgres". postgres is the database you connect to when you are not connecting to a database. In there is a table called pg_roles
.
使用这个命令:
psql -U pgadmin -d postgres -c 'select * from pg_roles'
它返回这样的:
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolconfig | oid
----------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+---------------+-----------+-------
postgres | t | t | t | t | t | t | t | -1 | ******** | | | 10
pgadmin | t | t | t | t | t | t | t | -1 | ******** | | | 16384
(2行)
看到这个答案:如何检查是否一个Postgres用户是否存在?
通过登录测试它
刚刚尝试使用提供的用户名/密码进行登录,并与一个try / catch围绕着它,如果你可以登录,然后它是有效的,否则是无效的。
Just try logging in with the supplied username/password and surround it with a try/catch, and if you can login then it is valid, otherwise it is not valid.
改变用户
您也许可以,如果你改变这种不存在的用户搭上一条错误消息:的 http://www.postgresql.org/docs/8.0/static/sql-alteruser.html
You might be able to catch an error message if you alter a user that doesn't exist: http://www.postgresql.org/docs/8.0/static/sql-alteruser.html
ALTER USER postgres WITH PASSWORD 'tmppassword';
删除并重新添加用户
如果您尝试删除并重新添加用户您可能能够赶上一个错误消息。所以,如果它是无效的,那么它会当您尝试删除一个非用户抛出一个错误。 http://www.postgresql.org/docs/8.0/static/sql -createuser.html
You might be able to catch an error message if you try to delete and re-add a user. So if it was invalid then it would have thrown an error when you try to delete a non user. http://www.postgresql.org/docs/8.0/static/sql-createuser.html
这篇关于验证PostgreSQL用户和密码数据库无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!