Postgres服务器在意外的Yosemite关闭后无法启动

时间:2015-07-22 15:03:26

标签: ruby-on-rails postgresql permissions server rails-postgresql

我正在尝试启动rails服务器,但我收到此错误...

/Users/kweihe/.rvm/gems/ruby-2.1.6/gems/activerecord-3.2.22/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

如果我查看我的postgres服务器进程,我会得到这个......

kweihe-mac:pmpaware-webapp kweihe$ ps auxw | grep postgres
kweihe          11687   0.0  0.0  2432772    636 s000  S+   10:56AM   0:00.00 grep postgres

所以我尝试了以下......

kweihe-mac:pmpaware-webapp kweihe$  rm -rf /usr/local/var/postgres
kweihe-mac:pmpaware-webapp kweihe$ initdb /usr/local/var/postgres 
The files belonging to this database system will be owned by user "kweihe".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/var/postgres ... initdb: could not change permissions of directory "/usr/local/var/postgres": Operation not permitted
kweihe-mac:pmpaware-webapp kweihe$ chmod 0700 /usr/local/var/postgres
chmod: Unable to change file mode on /usr/local/var/postgres: Operation not permitted

然后我检查了权限......

kweihe-mac:pmpaware-webapp kweihe$ cd /usr/local/var/
kweihe-mac:var kweihe$ ls -l
total 0
drwxr--r--  2 root  admin  68 Jul 22 10:59 postgres
kweihe-mac:var kweihe$ 

并尝试启用所有权限...

kweihe-mac:var kweihe$ chmod 777 postgres
chmod: Unable to change file mode on postgres: Operation not permitted

所以那时我几乎到了那里......

kweihe-mac:pmpaware-webapp kweihe$ rm -rf /usr/local/var/postgres
kweihe-mac:pmpaware-webapp kweihe$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "kweihe".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /usr/local/var/postgres/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 11413) running in data directory "/usr/local/var/postgres"?
child process exited with exit code 1
initdb: removing data directory "/usr/local/var/postgres"
could not remove file or directory "/usr/local/var/postgres": Directory not empty
initdb: failed to remove data directory

2 个答案:

答案 0 :(得分:3)

问题似乎是一个正在运行的launchctl进程,它取决于/usr/local/var/postgres。就我而言,解决方案是用

来阻止它

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

结束(删除以前部分创建的目录)

rm -rf /usr/local/var/postgres
initdb /usr/local/var/postgres

答案 1 :(得分:1)

<强>解决

这是我的工作流程说明

继续尝试奔跑但仍然失败

 initdb -D /usr/local/var/postgres/

postgres一直在/ usr / local / var /中生成一个具有不正确权限的新目录

解决方案:

brew uninstall postgres

重启电脑

目录/ usr / local / var / postgres以某种方式再次生成(具有不正确的权限)

检查postgres安装以确保它未安装

删除了该目录

重新启动计算机

brew install postgres

生成正确的postgres目录

postmaster.pid文件不在那里

尝试再次运行initdb - 失败

删除目录并重新初始化

kweihe-mac:~ kweihe$ rm -rf /usr/local/var/postgres/*
kweihe-mac:~ kweihe$ initdb  /usr/local/var/postgres 

创建角色农场

kweihe-mac:pmpaware-webapp kweihe$ psql
psql: FATAL:  database "kweihe" does not exist
kweihe-mac:pmpaware-webapp kweihe$ createdb
kweihe-mac:pmpaware-webapp kweihe$ psql
psql (9.4.4)
Type "help" for help.

kweihe=# create role farm with superuser;
CREATE ROLE
kweihe=# alter role farm with login;
ALTER ROLE
kweihe=# alter role farm with password '';
ALTER ROLE

启动了postgres服务器(生成postmaster.pid文件)

kweihe-mac:~ kweihe$ postgres -D /usr/local/var/postgres

安装pgcrypto扩展

create extension if not exists pgcrypto schema pg_catalog;

创建数据库

kweihe-mac:pmpaware-webapp kweihe$ bundle exec rake db:create
kweihe-mac:pmpaware-webapp kweihe$ bundle exec rake db:migrate
kweihe-mac:pmpaware-webapp kweihe$ bundle exec rake db:seed
相关问题