[英]Cannot connect to Postgresql server, locally
I have a Rails app that has been using sqlite3 for the DB. 我有一个Rails应用程序,一直在使用sqlite3作为数据库。 Deployed to Heroku. 部署到Heroku。 Then find out that Heroku recommends switching to PostgreSQL. 然后发现Heroku建议切换到PostgreSQL。 So now I'm trying to switch over without any luck. 所以现在我试图切换没有任何运气。 Was trying to use this Railscasts video for help . 试图使用这个Railscasts视频寻求帮助 。
I installed Homebrew. 我安装了Homebrew。 Installed PostgreSQL via Homebrew. 通过Homebrew安装PostgreSQL。 After installation, there was no mention of creating a username or password for postgres. 安装后,没有提到为postgres创建用户名或密码。
I edited my Gemfile to 我编辑了我的Gemfile
gem 'pg'
for both production and development and did bundle install
. 用于生产和开发,并进行bundle install
。
I edited my database.yml
file to this: 我编辑了我的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
Like Ryan says to do in the video, I try this command: 就像Ryan在视频中所说的那样,我尝试这个命令:
rake db:create:all
Only to get this error: 只是为了得到这个错误:
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"?
I do some more searching, and see that some tutorials show username and password included in the database.yml
file. 我做了一些搜索,看到一些教程显示了database.yml
文件中包含的用户名和密码。 I then find out how to setup a user for Postgresql 然后我找到了如何为Postgresql设置用户
After entering in the command $ createuser joe
, I was never given the options that the docs say you'll be asked. 输入$ createuser joe
命令后,我从未得到过文档说你会被问到的选项。 Such as "Shall the new role be a superuser? (y/n)" So really not sure if the user was created, but there wasn't any errors either. 例如“新角色应该是超级用户?(是/否)”所以真的不确定用户是否已创建,但也没有任何错误。
So, I'm assuming, after creating the user "joe", I reedited my database.yml
file to include the user field I just created: 所以,我假设,在创建用户“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
Only to still get the same error of not being able to connect. 只是仍然得到无法连接的相同错误。
I've ran the command 我跑了命令
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
to make sure the server is running as well. 确保服务器也在运行。
Just in case it's needed, when I run 以防万一需要,我跑的时候
which psql
I receive this: 我收到了这个:
/usr/local/bin/psql
Is there something that I'm missing? 有什么东西我不见了吗? The "database name" part of the database.yml file
. database.yml file
的“数据库名称”部分。 This is supposed to be a database already created somewhere, or does this file create the database when I run the rake db:create:all
command? 这应该是已在某处创建的数据库,或者当我运行rake db:create:all
命令时,此文件是否创建了数据库? I'm assuming the latter, so the name of the database doesn't matter? 我假设后者,所以数据库的名称无关紧要?
Lazy option: Add host: localhost
to your database.yml
. 延迟选项:将host: localhost
添加到您的database.yml
。
Only slightly less lazy option: Uninstall and reinstall the pg
gem. 只有稍微懒惰的选项:卸载并重新安装pg
gem。
What's going on here? 这里发生了什么? There are a number of ways for Postgres to already exist on your system, and the pg
gem will use their pg_config
output and build for their needs if you install the gem before installing your own copy of Postgres. Postgres有很多方法已经存在于你的系统上,如果你在安装自己的Postgres副本之前安装gem, pg
gem将使用它们的pg_config
输出并构建它们的需求。
In your case, it was built for the version included with some releases of Mac OS X, which uses a socket file at /var/pgsql_socket
. 在您的情况下,它是为某些版本的Mac OS X中包含的版本构建的,该版本在/var/pgsql_socket
使用套接字文件。
when you are installing Postgres via Homebrew it will add your username to postgres with an empty password. 当您通过Homebrew安装Postgres时,它会将您的用户名添加到postgres并使用空密码。
Just open a command prompt and type 只需打开命令提示符并键入即可
whoami
Then change your database.yml to your mac user name (whatever is returned from whoami) with empty password. 然后使用空密码将database.yml更改为您的mac用户名(无论是从whoami返回的)。
Maybe add host: localhost to you database.yml 也许将host:localhost添加到database.yml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.