【问题标题】:Postgres server not starting after unexpected Yosemite shutdown意外关闭优胜美地后 Postgres 服务器未启动
【发布时间】:2015-10-12 13:19:35
【问题描述】:

我正在尝试启动 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

【问题讨论】:

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


    【解决方案1】:

    看起来问题是一个正在运行的launchctl 进程,它依赖于/usr/local/var/postgres。就我而言,解决方案是用

    停止它

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

    结束(删除目录,以防它之前部分创建)

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

    【讨论】:

      【解决方案2】:

      已解决

      这是我的工作流程笔记

      一直尝试运行但一直失败

       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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-24
        • 1970-01-01
        • 2014-07-31
        相关资源
        最近更新 更多