2

My goal is to have a Postgres setup that I can use to develop my rails app and then push it to Heroku without having to change the database config file. Seems simple enough, right?

I've seen plenty of things on the internet about PostgreSQL 9.2.x not running on OS X 10.8, and I'm still struggling to get it working with Rails. Not realizing that a version of PSQL shipped with OS X, I set out to install it. (NOTE: I'm sort of debugging in circles, so things may not have worked as I expected.)

I've tried downloading and installing using the official graphical installer listed here, and I rebooted my Mac. I saw an extra user account was created, and I promptly deleted it. (I know, I know, probably what caused some heartache down the line, but it didn't seem reasonable to have an extra user account sitting around.)

Upon running my rails app, it couldn't connect to postgres. I tried running psql from Terminal.

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?`

I then proceeded to install Postgres with homebrew. I installed homebrew, then postgres. Same thing. I googled a bit and ran into a thread on the Postgres forum. What I got from it was that something changed between OS X 10.7 and 10.8, but that wasn't the cause of my problem.

Next, I tried using self-contained the Postgres.app. Now, I was able to create users and connect to the database. Rails was able to connect to Postgres, but complained that the database that I defined in config.yaml was absent. So I created it - or so I thought.

Running CREATE DATABASE my_db in the "self-contained" version of PSQL didn't work - even though without the self contained app, PSQL would have that socket error. I then tried creating a new user with write access. Nope. The CREATE was silently failing.

I tried adding localhost to my environment variable and it seemed to have solved the port error - but now Rails was having trouble finding Postgres again.

I uninstalled the brew version. I removed the enterprise tools et al. Still no luck. Reboot. Still no luck.

I just uninstalled brew and my config looks like this:

development:
  adapter: postgresql
  database: my_db
  pool: 5
  timeout: 5000
  host: localhost
  port: 5432

Rails says the database doesn't exist.

What do I need to do to get Postgres working with Rails 3.2.6 on Mac OS X 10.8.4 Mountain Lion?

4

1 回答 1

7

有很多方法可以达到您想要的目的,但这应该有效:

首先 - 删除任何现有的 PostgreSql 副本,如下所示:

brew update
brew uninstall postgresql

(假设 brew 更新没有问题)

我发现这删除了 ​​osx 10.8 附带的实例

如果您仍然有 Postgres 应用程序,请将其从应用程序中拖出并放入垃圾箱

然后我会重新启动,只是为了它......(我的旧 Windows 经验 :-)

然后安装最新的 postgres(或者你可以在这里指定一个版本)

brew install postgres

如果它抛出与 ossd-uuid 之类的错误有关的错误,那么只需执行

brew uninstall ossd-uuid && brew install ossd-uuid

那应该清除它

自制程序列出了一些有用的命令,例如将 postgres 设置为在启动时启动等

创建一个初始的 postgres 数据库

initdb /usr/local/var/postgres -E utf8

开始 postgres 由

postgres -D /usr/local/var/postgres

现在在您的终端提示符下,键入

psql postgres

使用您的 Mac 管理员密码,您应该得到一个提示(\q 退出):

postgres=#

将创建一个使用您的用户名的用户(但是,从记忆中,不是它的密码)所以现在设置

postgres=# alter user your-name with password 'anything';

如果成功,它将返回 ALTER ROLE(如果没有,则只是提示)

然后,如果您的 Rails 数据库是例如 dev_db,您可以为此创建一个用户:

postgres=# create role your_user with login createdb createrole password 'your_pass';

your_user 和 your_pass 将出现在您的 rails database.yaml 文件中(不确定 createdb 和 createrole 是否需要在那里,但这就是我所做的)

最后,您的 database.yaml 需要进行如下开发:

development:
  adapter: postgresql
  encoding: unicode
  database: dev_db
  pool: 5
  timeout: 5000
  host: localhost
  username: your_user
  password: your_pass

然后

rake db:create

这应该会创建一个空数据库,您的 Rails 迁移将填充该数据库

我希望 :-)

于 2013-09-24T20:57:01.473 回答