ruby-on-rails - psql:无法连接到服务器:连接被拒绝服务器是否在主机 "localhost"(::1) 上运行并在端口 5432 上接受 TCP/IP 连接?

标签 ruby-on-rails macos postgresql

很抱歉,我已经在 SO 帖子上跑了一天多了,我只是觉得我无处可去。我最近将我的软件更新为 Mavericks,我的 Rails 应用程序不再有效。特别是我的 psql 数据库不工作。当我在我的控制台中键入 psql 时,它返回“无法连接到服务器:连接被拒绝...”,当我运行服务器时,我得到一个带有相同文本的“PG 连接错误”。

这是我的 pg_hba.conf 文件:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     Brian_Liou                                trust
#host    replication     Brian_Liou        127.0.0.1/32            trust
#host    replication     Brian_Liou        ::1/128                 trust

这是我的 postgresql.conf

# FILE LOCATIONS
#------------------------------------------------------------------------------

# The default values of these variables are driven from the -D command-line
# option or PGDATA environment variable, represented here as ConfigDir.

#data_directory = 'ConfigDir'       # use data in another directory
                # (change requires restart)
#hba_file = 'ConfigDir/pg_hba.conf' # host-based authentication file
                # (change requires restart)
#ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file
                # (change requires restart)

# If external_pid_file is not explicitly set, no extra PID file is written.
#external_pid_file = ''         # write an extra PID file
                # (change requires restart)


#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

#listen_addresses = ‘*’             # what IP address(es) to listen on;
                # comma-separated list of addresses;
                # defaults to 'localhost'; use '*' for all
                # (change requires restart)
#port = 5432                # (change requires restart)
max_connections = 100           # (change requires restart)
# Note:  Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/tmp'   # comma-separated list of directories
                # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation
                # (change requires restart)
#bonjour = off              # advertise server via Bonjour
                # (change requires restart)
#bonjour_name = ''          # defaults to the computer name
                # (change requires restart)

# - Security and Authentication -

#authentication_timeout = 1min      # 1s-600s
#ssl = off              # (change requires restart)
#ssl_ciphers = 'DEFAULT:!LOW:!EXP:!MD5:@STRENGTH'   # allowed SSL ciphers
                # (change requires restart)
#ssl_renegotiation_limit = 512MB    # amount of data between renegotiations
#ssl_cert_file = 'server.crt'       # (change requires restart)
#ssl_key_file = 'server.key'        # (change requires restart)
#ssl_ca_file = ''           # (change requires restart)
#ssl_crl_file = ''          # (change requires restart)
#password_encryption = on
#db_user_namespace = off

# Kerberos and GSSAPI
#krb_server_keyfile = ''
#krb_srvname = 'postgres'       # (Kerberos only)
#krb_caseins_users = off

# - TCP Keepalives -
# see "man 7 tcp" for details

#tcp_keepalives_idle = 0        # TCP_KEEPIDLE, in seconds;
                # 0 selects the system default
#tcp_keepalives_interval = 0        # TCP_KEEPINTVL, in seconds;
                # 0 selects the system default
#tcp_keepalives_count = 0       # TCP_KEEPCNT;
                # 0 selects the system default

我尝试删除此文件夹:/usr/local/var/postgres,然后使用 brew 重新安装 postgresql。

我的 Bash 配置文件包括

export PGDATA=/usr/local/var/postgres
export PGHOST=localhost

我知道这个问题与 Mac 自动安装的 Postgres 和 brew 安装的 Postgres 有关,除此之外我完全迷路了。仅供引用,我也是一名 Rails/程序员新手。请让我知道我可以提供的任何其他信息来解决此问题!

最佳答案

我终于能够得到 friend 的个人帮助了!谢谢 Mark Miyashita!!!!

reset-postgres(){
  rm -rf /usr/local/var/postgres
  sudo sysctl -w kern.sysv.shmall=65536
  sudo sysctl -w kern.sysv.shmmax=16777216
  initdb /usr/local/var/postgres -E utf8
  sleep 2
  launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  cp /usr/local/Cellar/postgresql/9.3.4/homebrew.mxcl.postgresql.plist               ~/Library/LaunchAgents/
  launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  sleep 3
  createdb DATABASENAME_development
  sleep 1
}

可能必须根据您拥有的 postgres 更改版本号

关于ruby-on-rails - psql:无法连接到服务器:连接被拒绝服务器是否在主机 "localhost"(::1) 上运行并在端口 5432 上接受 TCP/IP 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22772427/

相关文章:

ruby-on-rails - 部署到 heroku 时出错

mysql - 如何使用标准输入在 Swift 3.0 中运行进程

postgresql - 限制数据库大小

sql - 选择批处理的行

javascript - Sequelize 迁移 - 在 Node 应用程序启动时自动运行

ruby-on-rails - rails : Default sort order for a rails model?

ruby-on-rails - Ruby On Rails 5.2 和多个数据库

ruby-on-rails - 如何为初始化变量创建验证?

objective-c - 'convertPoint' 的使用不明确

regex - 使用正则表达式和 bash 在 xidel 中为 xpath 表达式创建别名