Mac OSX 10.6.8
postgres网站说postgres 9.2+兼容Mac OSX 10.6+,所以我在这里下载了Mac OSX的9.2.4版本的安装程序:
http://www.enterprisedb.com/products-services-training/pgdownload
我接受所有默认设置,所以postgres被安装在目录:
/Library/PostgreSQL/9.2
(如果您正在为rails开发安装postgres,为了安装pg gem,您需要添加到您的PATH: http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx/#.UfkImuB6gy5)
现在,我正试图找出如何使用postgres。我发现一个stackoverflow线程说你可以用这个命令启动postgres:
~$ /Library/PostgreSQL/9.2/bin/pg_ctl start -D /Library/PostgreSQL/9.2/data -l postgres.log
但是这会产生这个错误:
pg_ctl:无法打开PID文件"/图书馆/PostgreSQL/9.2/数据/邮政局长。pid":权限被拒绝
如果我尝试用sudo执行相同的命令:
~$ sudo /Library/PostgreSQL/9.2/bin/pg_ctl start -D /Library/PostgreSQL/9.2/data -l postgres.log
我得到这个错误:
pg_ctl: cannot be run as root请以root身份登录(例如使用"su")(非特权)拥有服务器进程的用户。
这到底是什么意思?
另一个SO线程说,在响应这个错误时,你应该这样做:
$ sudo -u postgres bash
产生如下输出:
Password:
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
bash: /Users/7stud/.bashrc: Permission denied
bash-3.2$
这里的错误是怎么回事?
遇到和解决的错误列表:
-
shell-init:检索当前目录错误:getcwd:不能访问父目录:Permission denied
- pg_config错误
-
库未加载:libpq.5.dylib
-
fe_sendauth: no password供应
-
pg_ctl:没有指定数据库目录和环境变量PGDATA
-
pg_ctl: server does not shutdown
-
(PG:错误)
)预装:
当您单击.dmg文件开始安装时,会打开一个对话框窗口,安装程序旁边有一个README文件。读到。我按照规定更改了共享内存:
$ sudo vi /etc/sysctl.conf
On a MacBook Pro with 2GB of RAM, the author's sysctl.conf contains:
kern.sysv.shmmax=1610612736
kern.sysv.shmall=393216
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.maxprocperuid=512
kern.maxproc=2048
我将文件向下滚动到k,并将列出的每一行更改为指定的值。然后你需要重新启动你的电脑。
B)安装和使用PostgreSQL:
然后再次单击.dmg文件,然后单击安装程序并安装postgres。我接受所有的默认设置。
据我所知,postgres安装程序只给名为'postgres'的用户/Library/PostgreSQL/9.2目录权限。在postgres安装过程中,安装程序要求输入数据库超级用户的密码,如果你想知道数据库超级用户的名字是什么,它是'postgres'。
当你发出sudo命令时,你暂时切换到用户'root'。但是,root用户没有权限访问目录/Library/PostgreSQL/9.2。因此,必须使用su命令(切换用户)切换到postgres用户。但是为了发出su命令,您需要是根用户。明白了吗?
因此,每当您发出postgres命令时,您需要首先执行以下操作:$ cd /Library/PostgreSQL/9.2
$ sudo su postgres
Password: <normal sudo password>
像这样的错误:
~$ sudo su postgres
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
是由于您试图切换到postgres用户没有权限的目录下的用户postgres,这是,据我所知,每个目录除了/Library/PostgreSQL/9.2
正确切换到postgres用户后:
$ cd /Library/PostgreSQL/9.2
$ sudo su postgres
Password: <normal sudo password>
你会得到一个有趣的提示。在我的例子中,它看起来像这样:
bash-3.2$
如果您发出ls命令:
bash-3.2$ ls
bin installer scripts
data lib share
doc pgAdmin3.app stackbuilder.app
include pg_env.sh uninstall-postgresql.app
你可以推测你在/Library/PostgreSQL/9.2目录下。
enterpriseDB安装程序在完成安装后会自动为您启动postgres服务器。如果你需要启动服务器,例如你关闭了电脑,请参阅下面步骤5中的"你可以从命令行停止或启动服务器"。
C)使用pgAdmin3连接到服务器:
我花了几个小时试图弄清楚如何启动服务器,只是一般使用postgres没有运气。然后我碰巧在目录/Library/PostgreSQL/9.2中看到了一个叫pgAdmin3的东西。我点击它,一个有几个窗格的窗口打开了。
在pgAdmin3窗口的左窗格中,它说:
Server Groups
--Servers(1)
----PostgresSQL 9.2 (localhost:5432)
我点击'PostgreSQL 9.2 (localhost:5432)'来突出显示它,然后右键点击弹出一个菜单,我选择了连接。当出现提示时,我输入了数据库超级用户密码。
D)设置rails使用PostgreSQL:
然后我按照本教程中的说明进行rails设置:
https://pragtob.wordpress.com/2012/09/12/setting-up-postgresql-for-ruby-on-rails-on-linux/评论- 1142
如下:
1)第一步是创建一个新用户
如果你使用与mac用户名相同的名称,那么你不必向数据库添加用户名(或密码)。在rails应用程序中的Yml文件:
$ cd /Library/PostgreSQL/9.2
$ sudo su postgres
Password: <normal sudo password>
bash-3.2$ createuser --interactive 7stud
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
Password:<database superuser password>
bash-3.2$
创建一个没有密码的新用户。
接下来,在pgAdmin3中如果你点击:
---Login Roles(1)
将其突出显示,然后点击窗口顶部的"刷新所选对象"图标,您将看到显示的新用户。
创建用户的postgres文档在这里:
http://www.postgresql.org/docs/9.2/interactive/app-createuser.html
还有一个dropuser命令:
bash-3.2$ dropuser 7stud
2)然后添加'pg' gem到你的Gemfile,并注释掉sqlite gems。
3)然后尝试使用Bundler安装pg gem:
rails_projects/sample_app/$ bundle install --without production
当我尝试时,我得到了一个'pg_config'错误。为了修复这个错误,我遵循了下面的建议:
http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx/.UftQUOB6gy6
并将/Library/PostgreSQL/9.2添加到我的PATH(我在.bashrc.中执行所有的PATH操作)。不要忘记键入'source' .bashrc,或者退出终端并重新启动终端以使新的PATH生效。
然后我可以安装pg gem没有错误:
rails_projects/sample_app/$ bundle install --without production
rails_projects/sample_app/$ bundle update
rails_projects/sample_app/$ bundle install
(我真的不明白为什么您必须执行这三个命令,但这就是railstutuual所做的)
4)接下来改变你的配置/数据库。yml文件。
对于每个部分:测试、开发和生产,更改'适配器'和'数据库'行,并添加'编码'行,例如:
development:
adapter: postgresql
database: ArbitaryDatabaseName (e.g. sampleapp_dev)
encoding: utf8
pool: 5
timeout: 5000
确保所有三个数据库行指定不同的名称。
5)尝试创建数据库:
rails_projects/sample_app/$ bundle exec rake db:create:all
当我尝试的时候,我得到了错误:
dlopen(/Users/7stud/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/pg-0.15.1/lib/pg_ext.bundle, 9): Library not loaded: libpq.5.dylib
Library not loaded: libpq.5.dylib
Referenced from: /Users/7stud/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/pg-0.15.1/lib/pg_ext.bundle
库libpq.5。Dylib位于
/Library/PostgreSQL/9.2/lib
使用此处的建议:
https://github.com/PostgresApp/PostgresApp/issues/109
我通过将以下内容添加到我的.bashrc文件(或者您可以将其放入.bash_profile中)来解决该错误:
export DYLD_FALLBACK_LIBRARY_PATH="/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_PATH"
(记住输入'source' .bashrc或退出终端并启动一个新的终端窗口)
然后我尝试重新创建数据库:
rails_projects/sample_app/$ bundle exec rake db:create:all
fe_sendauth: no password supplied …
要解决这个爆炸错误,您需要更改pg_hba.conf文件,该文件位于目录
中/Library/PostgreSQL/9.2/data
你不能使用Finder进入那个目录——你必须使用postgres用户进入那个目录:
$ cd /Library/PostgreSQL/9.2
$ sudo su postgres
Password: <normal sudo password>
bash-3.2$ cd data
bash-3.2$ ls
PG_VERSION pg_hba.conf pg_notify pg_subtrans postgresql.conf
base pg_ident.conf pg_serial pg_tblspc postmaster.opts
global pg_log pg_snapshots pg_twophase postmaster.pid
pg_clog pg_multixact pg_stat_tmp pg_xlog
bash-3.2$ mvim pg_hba.conf
(or instead of mvim open with another text editor, e.g. vim)
在.conf文件中,你需要将'md5'更改为'trust':
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust #md5
# IPv4 local connections:
host all all 127.0.0.1/32 trust #md5
# IPv6 local connections:
host all all ::1/128 trust #md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres md5
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
访问方法'md5'表示数据库正在等待指定数据库用户的加密密码。但是当您创建新用户时,您创建它时没有密码。访问方法'trust'表示数据库不需要密码。
然后我尝试重新创建数据库:
rails_projects/sample_app/$ bundle exec rake db:create:all
fe_sendauth: no password supplied …
为了让服务器读取我在.conf文件中所做的更改,我在pgAdmin3中断开了与服务器的连接:
Server Groups
--Servers(1)
----PostgresSQL 9.2 (localhost:5432)
Click on PostgresSQL 9.2 (localhost:5432) to highlight it,
right click, then select Disconnect,
then right click and select Connect.
但显然这不起作用。断开连接并重新连接到服务器不会导致服务器重新加载.conf文件。要让服务器重新加载.conf文件,可以在pgAdmin3中右键单击PostgresSQL 9.2 (localhost:5432)
并选择:
Reload configuration
或者您可以先停止然后从命令行启动服务器:
(注意以下命令假设您已经切换到postgres用户,请参阅上面的"安装和使用PostgreSQL")
bash-3.2$ cd /Library/PostgreSQL/9.2/
bash-3.2$ pg_ctl stop (First disconnect from the server in pgAdmin3)
然而,这产生了错误:
pg_ctl: no database directory specified and environment variable PGDATA unset
Try "pg_ctl --help" for more information.
我这样解决了这个错误:
bash-3.2$ export PGDATA=/Library/PostgreSQL/9.2/data
bash-3.2$ echo $PGDATA
/Library/PostgreSQL/9.2/data
bash-3.2$ pg_ctl stop
waiting for server to shut down.... done
server stopped
bash-3.2$ pg_ctl start
server starting
如果在pgAdmin3中不首先断开与服务器的连接,则会得到如下输出:
bash-3.2$ pg_ctl stop
waiting for server to shut down............................................................... failed
pg_ctl: server does not shut down
HINT: The "-m fast" option immediately disconnects sessions rather than
waiting for session-initiated disconnection.
bash-3.2$
后来,当我打开rails控制台时,我无法关闭服务器。我使用User.create(....)
向数据库添加了一个User,但是我忘记事先启动postgres,所以我想知道该记录将会发生什么。由于某种原因,postgress服务器已经在我的系统上运行了(似乎每次我启动计算机时都会启动postgress——解决方法如下)。接下来,我试图停止服务器,但失败了。关闭rails控制台后,服务器停止。
最后,没有错误:
~/rails_projects/sample_app4_0$ bundle exec rake db:create:all
~/rails_projects/sample_app4_0$ bundle exec rake db:migrate
~/rails_projects/sample_app4_0
我猜这意味着db被创建了。如果查看pgAdmin3,您应该能够看到三个新数据库(尽管它们上面有红色的x)。如果看不到它们,右键单击数据库目录(在pgAdmin3中)并选择Refresh。
编辑:我后来遇到了一些问题。要完全测试是否工作正常,请参见这里:如何让我的rails应用程序使用我的postgresql数据库?
6)警卫/叉勺
当我启动我的守卫/叉组合:
~/rails_projects/sample_app4_0$ bundle exec guard
我得到这个错误:
...
...
Preloading Rails environment
Loading Spork.prefork block...
FATAL: the database system is shutting down
(PG::Error)
我通过启动postgres服务器,并使用pgAdmin3连接到服务器来修复这个问题。
最终,我想出了如何让postgres服务器在每次启动计算机时都不启动。我必须做两个改动:
1)$ sudo vim /Library/LaunchDaemons/com.edb.launchd.postgresql-9.2.plist
用你的postgres版本代替9.2
2)找到RunAtLoad
3)将下一行从<true/>
改为<false/>
https://superuser.com/questions/244589/prevent-postgresql-from-running-at-startup
仅仅这些步骤对我不起作用。然后我发现了这个建议:
4)$ sudo launchctl unload -w /Library/LaunchDaemons/com.edb.launchd.postgresql-9.2.plist
用你的postgres版本代替9.2
http://www.postgresql.org/message-id/flat/4B41FD48.7030009@enterprisedb.com # 4 b41fd48.7030009@enterprisedb.com