2017-09-26 140 views
1

我是Docker的新手,想用Postgres運行它。然後,我嘗試運行其他人但不是我的python測試用例。Docker角色創建錯誤 - 角色___不存在

下面的錯誤告訴我,泊塢窗可奮力創建角色foo

E sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: role "foo" does not exist

另一個錯誤:

E psycopg2.OperationalError: FATAL: role "foo" does not exist

PostgreSQL的版本:mydb=# SELECT version();

PostgreSQL 9.6.5 on x86_64-apple-darwin16.7.0, compiled by Apple LLVM version 8.1.0 (clang-802.0.42), 64-bit 
(1 row) 

泊塢窗版本

Version 17.06.2-ce-mac27 (19124) 

這個腳本運行碼頭工人:

docker run -p 5432:5432 --env POSTGRES_PASSWORD="bar" --env POSTGRES_USER="foo" --env POSTGRES_DB="mydb" postgres 

從腳本輸出:正如你所看到的,它說CREATE ROLE

The files belonging to this database system will be owned by user "postgres". 
This user must also own the server process. 

The database cluster will be initialized with locale "en_US.utf8". 
The default database encoding has accordingly been set to "UTF8". 
The default text search configuration will be set to "english". 

Data page checksums are disabled. 

fixing permissions on existing directory /var/lib/postgresql/data ... 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 

Success. You can now start the database server using: 

    pg_ctl -D /var/lib/postgresql/data -l logfile start 


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. 
waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address 
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. 
LOG: database system was shut down at 2017-09-26 21:28:17 UTC 
LOG: MultiXact member wraparound protections are now enabled 
LOG: database system is ready to accept connections 
LOG: autovacuum launcher started 
done 
server started 
CREATE DATABASE 

CREATE ROLE 


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* 

LOG: received fast shutdown request 
LOG: aborting any active transactions 
LOG: autovacuum launcher shutting down 
waiting for server to shut down....LOG: shutting down 
LOG: database system is shut down 
done 
server stopped 

PostgreSQL init process complete; ready for start up. 

LOG: database system was shut down at 2017-09-26 21:28:19 UTC 
LOG: MultiXact member wraparound protections are now enabled 
LOG: database system is ready to accept connections 
LOG: autovacuum launcher started 

編輯

CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS      PORTS     NAMES 
bbab7eb98fcf  postgres   "docker-entrypoint..." 10 seconds ago  Up 11 seconds    0.0.0.0:5432->5432/tcp hardcore_wilson 
a6a1e2e313b8  postgres   "docker-entrypoint..." 16 minutes ago  Exited (0) 6 minutes ago       nervous_banach 

回答

1

看起來像嘗試本地套接字連接的psycopg2。你會看到下面的異常,如果有人在連接TCP:

password authentication failed for user "foo"

和容器日誌中的錯誤:

FATAL: password authentication failed for user "foo" 
DETAIL: Role "foo" does not exist. 
    Connection matched pg_hba.conf line 95: "host all all all md5" 

確保host連接參數設置。如果沒有設置psycopg2將回退到使用UNIX套接字。

編輯:

看來,當通過localhost連接參數psycopg2會更喜歡連接的Unix套接字。如果有兩個運行postgres的實例,一個監聽UNIX套接字,一個監聽TCP端口5432,則psycopg2將在傳遞像postgresql://localhost:5432這樣的url時通過UNIX域套接字進行連接。

+0

Teppic,應該在哪裏設置主機連接參數?這是Docker連接字符串嗎?並且'host'參數應該設置爲'localhost'?基本上,連接字符串應該是這樣的:'docker run -p 5432:5432 --env POSTGRES_PASSWORD =「bar」--env POSTGRES_USER =「foo」--env POSTGRES_DB =「mydb」--env POSTGRES_HOST =「localhost」 postgres' – Growler

+0

您的python測試用例用於連接的參數。 – teppic

+0

是的,它似乎被設置爲'localhost',這正是我想要的,對嗎? 'dsn ='dbname = travis user = foo password = bar host = localhost port = 5432',database ='mydb',user ='foo',password ='bar',host ='localhost',port = 5432,connection_factory =無,cursor_factory =無 async = False,kwargs = {},items = [('dbname','mydb'),('user','foo'),('password','bar'),( 'host','localhost'),('port',5432)]' – Growler