postgres服务器已启动但无法连接到服务器

时间:2013-11-29 03:49:14

标签: postgresql sinatra

在我丢失了计算机上的所有数据后(必须将Apple商店删除的所有内容都删除),我使用Homebrew在我的Mac上安装了postgres,命令为brew install postgres。然后我启动了服务器(确认服务器已启动)并尝试使用命令

为使用postgres的Sinatra项目创建数据库
createdb db_development

但是,我收到以下错误:

    createdb: could not connect to database postgres: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

在线搜索后,我发现可能必须运行以下initdb命令

initdb /usr/local/var/postgres

但是当我这样做时,我收到了这条消息

initdb /usr/local/var/postgres
The files belonging to this database system will be owned by user "braindead".
This user must also own the server process.

The database cluster will be initialized with locale "en_CA.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.

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

那么,为什么postgres会在我收到此错误could not connect to database postgres: could not connect to server: No such file or directory的同时确认服务器已启动,我该如何解决?

该项目的READ.me说

bundle install
createdb db_development
rake db:migrate

2 个答案:

答案 0 :(得分:1)

它要么尝试连接到MacOS附带的postgres,要么未设置PG环境变量。 尝试:  createdb -h localhost db_development

对于postgres,这些是postgres environment variables。另外PostgresApp是在Mac上使用Postgresql的一种非常方便的方法

答案 1 :(得分:0)

Postgres在postgresql.conf中有一个“unix_socket_directory”设置 请验证此设置是否存在以及目录本身是否存在,然后重新启动服务器。如果它不存在,请将其设置为/ var / run(或其他存在的目录),然后重新启动服务器。