2017-08-01 358 views
0

我想创建一个PostgreSQL数据库,并能够以创建时指定的用户身份进行连接。因此我使用initdbcreatedb。然而,initdb --user/-U选项似乎被忽略:如何指定PostgreSQL数据库的超级用户?

> cd `mktemp -d` 
> /usr/lib/postgresql/9.6/bin/initdb --user=user1 test1 
The files belonging to this database system will be owned by user "richter". 
This user must also own the server process. 

The database cluster will be initialized with locale "de_DE.UTF-8". 
The default database encoding has accordingly been set to "UTF8". 
The default text search configuration will be set to "german". 

Data page checksums are disabled. 

creating directory test1 ... ok 
creating subdirectories ... ok 
selecting default max_connections ... 100 
selecting default shared_buffers ... 128MB 
selecting dynamic shared memory implementation ... posix 
creating configuration files ... ok 
running bootstrap script ... ok 
performing post-bootstrap initialization ... ok 
syncing data to disk ... ok 

WARNING: enabling "trust" authentication for local connections 
You can change this by editing pg_hba.conf or using the option -A, or 
--auth-local and --auth-host, the next time you run initdb. 

Success. You can now start the database server using: 

    /usr/lib/postgresql/9.6/bin/pg_ctl -D test1 -l logfile start 

因为user1是不具备的postgres命令启动服务器后createdb的连接失败。 richter是执行该命令的用户。

我在Ubuntu 17.04上使用PostgreSQL 9.6。

回答

1

两件事情:

  1. 你混合了数据库用户和操作系统用户。

    由于您运行initdb作为操作系统用户richter,该用户将拥有数据库文件和数据库服务器进程。

    用户user1是数据库超级用户。

  2. 你必须与psqlcreatedb--username-U选择权和其他PostgreSQL客户端应用程序指定数据库用户。

你应该表现出与其说“连接失败”命令和错误消息。

+0

谢谢。我认为长选项中的转义用户名是困难的 - 我没有系统地调查所有不包含单引号和双引号的组合,但是使用没有引号的'-U'对我来说工作得很好。 –

+1

这里有一个忠告:对于数据库对象名称(包括用户),始终只使用小写ASCII字符,数字和下划线。你会为自己节省很多麻烦,而且你永远不需要引用或逃避任何事情。 –

相关问题