[英]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.