问题描述
Mac OSX 10.6.8
Mac OSX 10.6.8
postgres 网站说 postgres 9.2+ 与 Mac OSX 10.6+ 兼容,所以我在这里下载了 Mac OSX 安装程序的 9.2.4 版本:
The postgres website says postgres 9.2+ is compatible with Mac OSX 10.6+, so I downloaded the 9.2.4 version of the installer for Mac OSX here:
http://www.enterprisedb.com/products-services-training/pgdownload
我接受了所有的默认值,所以 postgres 安装在目录中:
I accepted all the defaults, so postgres was installed in the directory:
/Library/PostgreSQL/9.2
(如果您正在为 Rails 开发安装 postgres,为了安装 pg gem,您需要添加到您的 PATH 中:http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx/#.UfkImuB6gy5一>)
(If you are installing postgres for rails development, in order to install the pg gem you need to add to your PATH: http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx/#.UfkImuB6gy5)
现在,我想弄清楚如何使用 postgres.我发现一个 stackoverflow 线程说你可以用这个命令启动 postgres:
Now, I am trying to figure out how to use postgres. I found a stackoverflow thread that said you can start postgres with this command:
~$ /Library/PostgreSQL/9.2/bin/pg_ctl start -D /Library/PostgreSQL/9.2/data -l postgres.log
但这会产生此错误:
pg_ctl: 无法打开 PID 文件/Library/PostgreSQL/9.2/data/postmaster.pid":权限被拒绝
如果我用 sudo 尝试相同的命令:
If I try the same command with sudo:
~$ sudo /Library/PostgreSQL/9.2/bin/pg_ctl start -D /Library/PostgreSQL/9.2/data -l postgres.log
我收到此错误:
pg_ctl: 不能以 root 身份运行 请以用户身份登录(例如使用su")将拥有服务器进程的(非特权)用户.
这到底是什么意思??
另一个 SO 线程说,为了响应该错误,您应该这样做:
Another SO thread says that in response to that error, you should do this:
$ 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$
那里的错误是怎么回事?
What's going on with the error there?
推荐答案
遇到并解决的错误列表:
List of errors encountered and solved:
shell-init:检索当前目录时出错:getcwd:无法访问父目录:权限被拒绝
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
pg_config 错误
pg_config error
库未加载:libpq.5.dylib
Library not loaded: libpq.5.dylib
fe_sendauth:未提供密码
fe_sendauth: no password supplied
pg_ctl:未指定数据库目录且未设置环境变量 PGDATA
pg_ctl: no database directory specified and environment variable PGDATA unset
pg_ctl:服务器没有关闭
pg_ctl: server does not shut down
(PG::Error)
(PG::Error)
A) 预安装:
当您单击 .dmg 文件开始安装时,会打开一个对话框窗口,并且安装程序旁边有一个 README 文件.读那个.我按照规定更改了共享内存:
When you click on the .dmg file to begin installation, a dialog window opens, and there is a README file alongside the installer. Read that. I changed the shared memory as prescribed:
$ 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 并将列出的每一行更改为指定的值.然后您需要重新启动计算机.
I scrolled down the file to the k's and changed each of the listed lines to the specified values. Then you need to reboot your computer.
B) 安装和使用 PostgreSQL:
然后再次点击.dmg文件,然后点击安装程序并安装postgres.我接受了所有默认设置.
Then click on the .dmg file again, then click on the installer and install postgres. I accepted all the defaults.
据我所知,postgres 安装程序只将目录/Library/PostgreSQL/9.2 的权限授予名为postgres"的用户.在 postgres 安装过程中,安装程序要求输入数据库超级用户的密码,如果您想知道数据库超级用户的名字是什么,那就是postgres".
As far as I can tell, the postgres installer only gives permissions for the directory /Library/PostgreSQL/9.2 to a user named 'postgres'. During the postgres installation, the installer asked for a password for the database superuser, and if you wondered what the database superuser's name was, it's 'postgres'.
当您发出 sudo 命令时,您将暂时更改为用户root".但是,root 用户无权访问目录/Library/PostgreSQL/9.2.所以必须使用su命令(切换用户)来切换到postgres用户.但是为了发出 su 命令,您需要是 root 用户.了解?
When you issue the sudo command, you are temporarily changing to the user 'root'. However, the root user does not have access to the dir /Library/PostgreSQL/9.2. So you have to use the su command (switch user) to change to the postgres user. But in order to issue the su command, you need to be the root user. Got that?
因此,每当您发出 postgres 命令时,您都需要先执行此操作:
As a result, whenever you issue postgres commands you need to do this first:
$ 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 之外的每个目录
are caused by the fact that you are trying to switch to the user postgres in a directory where the postgres user does not have permissions, which is, as far as I can tell, every directory except /Library/PostgreSQL/9.2
在你正确切换到 postgres 用户之后:
After you correctly switch to the postgres user:
$ cd /Library/PostgreSQL/9.2
$ sudo su postgres
Password: <normal sudo password>
你会得到一个看起来很有趣的提示.就我而言,它看起来像这样:
you get a funny looking prompt. In my case, it looks like this:
bash-3.2$
如果您发出 ls 命令:
If you issue the ls command:
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 目录中.
you can surmise that you are in the /Library/PostgreSQL/9.2 directory.
安装完成后,enterpriseDB 安装程序会自动为您启动 postgres 服务器.如果您需要启动服务器,例如您关闭了计算机,请参阅下面第 5 步中的您可以从命令行停止或启动服务器".
The enterpriseDB installer automatically starts the postgres server for you when it is done with the installation. If you need to start the server, e.g. you turned your computer off, see "you can stop or start the server from the command line" in step 5 below.
C) 使用 pgAdmin3 连接到服务器:
我花了几个小时试图弄清楚如何启动服务器,并且通常只使用 postgres 而没有运气.然后我碰巧在目录/Library/PostgreSQL/9.2 中看到了一个叫做 pgAdmin3 的东西.我点击它,打开了一个带有多个窗格的窗口.
C) Connecting to the server using pgAdmin3:
I spent hours trying to figure out how to start the server and just generally use postgres with no luck. Then I happened to see something called pgAdmin3 in the dir /Library/PostgreSQL/9.2. I clicked on that, and a window with several panes opened up.
在 pgAdmin3 窗口的左窗格中,它说:
In the left pane of the pgAdmin3 window, it says:
Server Groups
--Servers(1)
----PostgresSQL 9.2 (localhost:5432)
我单击PostgreSQL 9.2 (localhost:5432)"以突出显示它,然后我右键单击以显示一个菜单,我选择了连接".出现提示时,我输入了数据库超级用户密码.
I clicked on 'PostgreSQL 9.2 (localhost:5432)' to highlight it, and then I right clicked to bring up a menu, and I chose Connect. When prompted, I entered the database superuser password.
D) 设置 rails 以使用 PostgreSQL:
然后我按照本教程中的说明获取 rails 设置:
D) Setting up rails to use PostgreSQL:
Then I followed the directions in this tutorial for getting rails setup:
如下:
1) 第一步是创建一个新用户.
如果您使用与您的 mac 用户名相同的名称,那么您不必在您的 rails 应用程序中的 database.yml 文件中添加用户名(或密码):
1) The first step is creating a new user.
If you use the same name as your mac user name, then you don't have to add a username (or a password) to the database.yml file in your rails app:
$ 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$
这会创建一个没有密码的新用户.
That creates a new user with no password.
接下来,在 pgAdmin3 中,如果您点击:
Next, in pgAdmin3 if you click on:
---Login Roles(1)
突出显示它,然后单击窗口顶部的刷新所选对象"图标,您将看到显示的新用户.
to highlight it, and then click on the icon "Refresh the selected object" at the top of the window, you will see the new user displayed.
createuser 的 postgres 文档在这里:
The postgres docs for createuser are here:
http://www.postgresql.org/docs/9.2/interactive/app-createuser.html
还有一个 dropuser 命令:
There is also a dropuser command:
bash-3.2$ dropuser 7stud
2) 然后将pg" gem 添加到您的 Gemfile 中,并注释掉 sqlite gem.
2) Then add the 'pg' gem to your Gemfile, and comment out the sqlite gems.
3) 然后尝试使用 Bundler 安装 pg gem:
rails_projects/sample_app/$ bundle install --without production
当我尝试这样做时,我收到了pg_config"错误.为了解决这个错误,我遵循了这里的建议:
When I tried that, I got a 'pg_config' error. To fix that error, I followed the advice here:
http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx/#.UftQUOB6gy6
并将/Library/PostgreSQL/9.2 添加到我的 PATH(我在 .bashrc 中执行所有 PATH 操作).不要忘记 'source' .bashrc 或退出终端并重新启动终端以使新的 PATH 生效.
and added /Library/PostgreSQL/9.2 to my PATH (I do all PATH manipulations in .bashrc.). Don't forget to 'source' .bashrc or quit Terminal and restart Terminal for the new PATH to take effect.
然后我就可以毫无错误地安装 pg gem:
Then I was able to install the pg gem without error:
rails_projects/sample_app/$ bundle install --without production
rails_projects/sample_app/$ bundle update
rails_projects/sample_app/$ bundle install
(我真的不明白为什么你必须执行所有这三个命令,但这就是 railstutorial 所做的.)
(I don't really understand why you have to do all three of those commands, but that is what railstutorial does.)
4) 接下来更改您的 config/database.yml 文件.
对于每个部分:测试、开发和生产更改适配器"和数据库"行,并添加编码"行,例如:
4) Next change your config/database.yml file.
For each of the sections: test, development, and production change the 'adapter' and 'database' lines, and add an 'encoding' line, e.g.:
development:
adapter: postgresql
database: ArbitaryDatabaseName (e.g. sampleapp_dev)
encoding: utf8
pool: 5
timeout: 5000
确保所有三个数据库行都指定了不同的名称.
Make sure all three database lines specify a different name.
5) 然后尝试创建数据库:
rails_projects/sample_app/$ bundle exec rake db:create:all
当我尝试这样做时,出现错误:
When I tried that, I got the error:
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 位于
The library libpq.5.dylib is located in
/Library/PostgreSQL/9.2/lib
使用此处的建议:
https://github.com/PostgresApp/PostgresApp/issues/109
我通过将以下内容添加到我的 .bashrc 文件(或者您可以将它放在 .bash_profile 中)解决了该错误:
I solved that error by adding the following to my .bashrc file(or you can put it in .bash_profile):
export DYLD_FALLBACK_LIBRARY_PATH="/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_PATH"
(记住'source' .bashrc 或退出终端并启动一个新的终端窗口.)
(Remember to 'source' .bashrc or exit Terminal and start a new Terminal window.)
然后我再次尝试创建数据库:
Then I tried to create the databases again:
rails_projects/sample_app/$ bundle exec rake db:create:all
fe_sendauth: no password supplied …
要解决那个爆掉的错误,您需要更改目录中的 pg_hba.conf 文件
To solve that blasted error, you need to change the pg_hba.conf file, which is in the directory
/Library/PostgreSQL/9.2/data
你不能使用 Finder 进入那个目录——你必须使用 postgres 用户进入那个目录:
You can't go into that directory using Finder--you have to use the postgres user to go into that directory:
$ 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':
In the .conf file you need to change 'md5' to '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"表示数据库需要指定数据库用户的加密密码.但是当你创建新用户时,你创建的时候没有密码.访问 METHOD 'trust' 意味着数据库不需要密码.
The access METHOD 'md5' means the database is expecting an encrypted password for the specified database users. But when you created the new user, you created it without a password. The access METHOD 'trust' means the database does not expect a password.
然后我再次尝试创建数据库:
Then I tried to create the databases again:
rails_projects/sample_app/$ bundle exec rake db:create:all
fe_sendauth: no password supplied …
为了让服务器读取我在 .conf 文件中所做的更改,我在 pgAdmin3 中断开了与服务器的连接:
In an attempt to get the server to read the changes I made in the .conf file, I disconnected from the server in 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)
并选择:
But apparently that wasn't working. Disconnecting and reconnecting to the server does not cause the server to reload the .conf file. To get the server to reload the .conf file, in pgAdmin3 you can right click on PostgresSQL 9.2 (localhost:5432)
and select:
Reload configuration
或者您可以从命令行停止然后启动服务器:
(注意以下命令假设您已经切换到 postgres 用户,请参阅上面的安装和使用 PostgreSQL".)
Or you can stop and then start the server from the command line:
(Note the following commands assume you have already switched to the postgres user, see "Installing and using PostgreSQL" above.)
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 中断开与服务器的连接,你会得到输出:
If you don't disconnect from the server first in pgAdmin3, you get the output:
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(....)
向数据库添加了一个用户,但我忘记事先启动 postgres,所以我想知道该记录会发生什么.出于某种原因,postgress 服务器已经在我的系统上运行(似乎每次我启动计算机时 postgress 都会启动——解决方案如下).接下来,我试图停止服务器,但我无法停止.关闭 rails 控制台后,服务器停止.
Later, I was unable to shut down the server when I had the rails console open. I added a User to the database using User.create(....)
, and I forgot to start postgres beforehand, so I wondered what was going to happen to that record. For some reason, the postgress server was already running on my system(it seems that postgress starts every time I boot my computer--solved below). Next, I tried to stop the server, and I was unable to. After closing the rails console, the server stopped.
最后,没有错误:
~/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
我猜这意味着创建了数据库.如果您查看 pgAdmin3,您应该能够看到三个新数据库(尽管它们上面有红色的 x).如果您看不到它们,请右键单击数据库目录(在 pgAdmin3 中)并选择刷新.
I guess that means the db's were created. If you look in pgAdmin3, you should be able to see three new databases (although they have red x's on them). If you can't see them, right click on the Databases directory(in pgAdmin3) and select Refresh.
我后来遇到了一些问题.要全面测试一切是否正常,请参阅此处:
I ran into some problems later. To fully test if things are working, see here:
我如何获得我的rails 应用程序使用我的 postgresql 数据库?
6) 后卫/Spork
当我开始使用 Guard/Spork 组合时:
6) Guard/Spork
When I started up my Guard/Spork combination:
~/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 连接到服务器来解决这个问题.
I fixed that by starting the postgres server, and using pgAdmin3 to connect to the server.
最终,我想出了如何防止每次启动计算机时都启动 postgres 服务器.我必须进行两项更改:
Eventually, I figured out how to keep the postgres server from starting every time I boot my computer. I had to make two changes:
1) $ sudo vim/Library/LaunchDaemons/com.edb.launchd.postgresql-9.2.plist
用你的 postgres 版本代替 9.2.
1) $ sudo vim /Library/LaunchDaemons/com.edb.launchd.postgresql-9.2.plist
Substitute your version of postgres in place of 9.2.
2) 找到 RunAtLoad 行
2) Locate the line RunAtLoad
3) 将下一行从 更改为
3) Change the next line from <true/>
to <false/>
https://superuser.com/questions/244589/prevent-postgresql-from- 启动时运行
仅凭这些步骤对我不起作用.然后我找到了这个建议:
Those steps alone did not work for me. Then I found this advice:
4) $ sudo launchctl unload -w/Library/LaunchDaemons/com.edb.launchd.postgresql-9.2.plist
用你的 postgres 版本代替 9.2.
Substitute your version of postgres in place of 9.2.
http:///www.postgresql.org/message-id/flat/[email protected]#[email protected]
这篇关于如何在 Mac OSX 10.6.8 上启动 enterpiseDB PostgreSQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!