[英]Cannot connect to Postgresql server, locally
我有一個Rails應用程序,一直在使用sqlite3作為數據庫。 部署到Heroku。 然后發現Heroku建議切換到PostgreSQL。 所以現在我試圖切換沒有任何運氣。 試圖使用這個Railscasts視頻尋求幫助 。
我安裝了Homebrew。 通過Homebrew安裝PostgreSQL。 安裝后,沒有提到為postgres創建用戶名或密碼。
我編輯了我的Gemfile
gem 'pg'
用於生產和開發,並進行bundle install
。
我編輯了我的database.yml
文件到這個:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
production:
adapter: postgresql
database: isement_production
encoding: unicode
pool: 5
timeout: 5000
就像Ryan在視頻中所說的那樣,我嘗試這個命令:
rake db:create:all
只是為了得到這個錯誤:
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
我做了一些搜索,看到一些教程顯示了database.yml
文件中包含的用戶名和密碼。 然后我找到了如何為Postgresql設置用戶
輸入$ createuser joe
命令后,我從未得到過文檔說你會被問到的選項。 例如“新角色應該是超級用戶?(是/否)”所以真的不確定用戶是否已創建,但也沒有任何錯誤。
所以,我假設,在創建用戶“joe”之后,我重新編寫了我的database.yml
文件以包含我剛剛創建的用戶字段:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
username: joe
password:
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
只是仍然得到無法連接的相同錯誤。
我跑了命令
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
確保服務器也在運行。
以防萬一需要,我跑的時候
which psql
我收到了這個:
/usr/local/bin/psql
有什么東西我不見了嗎? database.yml file
的“數據庫名稱”部分。 這應該是已在某處創建的數據庫,或者當我運行rake db:create:all
命令時,此文件是否創建了數據庫? 我假設后者,所以數據庫的名稱無關緊要?
延遲選項:將host: localhost
添加到您的database.yml
。
只有稍微懶惰的選項:卸載並重新安裝pg
gem。
這里發生了什么? Postgres有很多方法已經存在於你的系統上,如果你在安裝自己的Postgres副本之前安裝gem, pg
gem將使用它們的pg_config
輸出並構建它們的需求。
在您的情況下,它是為某些版本的Mac OS X中包含的版本構建的,該版本在/var/pgsql_socket
使用套接字文件。
當您通過Homebrew安裝Postgres時,它會將您的用戶名添加到postgres並使用空密碼。
只需打開命令提示符並鍵入即可
whoami
然后使用空密碼將database.yml更改為您的mac用戶名(無論是從whoami返回的)。
也許將host:localhost添加到database.yml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.