2017-08-01 355 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字符,數字和下劃線。你會爲自己節省很多麻煩,而且你永遠不需要引用或逃避任何事情。 –

相關問題