Postgres Database Local Installation / Running

2019-08-21 23:19发布

问题:

Trying to setup Postgres / PostGIS on OSX Mountain Lion to use with Django. However, something is wrong and I am not sure how to fix it.

$ createdb geo
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"?

$ postgres -D /usr/local/var/postgres
FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 221) running in data directory "/usr/local/var/postgres"?

$ pg_ctl -D /usr/local/var/postgres -l logfile start
pg_ctl: another server might be running; trying to start server anyway
server starting

Then I found this gist that said how to fix it on lion. However it did not work for me:

$ kill -9 221
$ pg_ctl -D /usr/local/var/postgres stop
waiting for server to shut down.... done
server stopped

$ pg_ctl -D /usr/local/var/postgres start
pg_ctl: another server might be running; trying to start server anyway
server starting
FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 1436) running in data directory "/usr/local/var/postgres"?

Another attempt:

$ cd /usr/local/var/postgres
$ mv postmaster.pid postmaster.backup
$ pg_ctl -D /usr/local/var/postgres start
server starting

$ LOG:  could not bind IPv6 socket: Address already in use
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG:  could not bind IPv4 socket: Address already in use
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG:  could not bind IPv6 socket: Address already in use
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
WARNING:  could not create listen socket for "localhost"
FATAL:  could not create any TCP/IP sockets

Has anyone successfully done this with mountain lion? If so, what am I missing? Thanks for your help with this issue!

回答1:

This message:

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"?

indicates that your PostgreSQL server doesn't use /var/pgsql_socket as its Unix domain socket directory, which is the rendez-vous point between the server and the client. This is quite common, since only PostgreSQL shipped by Apple with OSX server uses that path. Incidentally, this is why the createdb command as shipped by Apple tries to connect to the server as configured by Apple.

From that point, there are a lot of different solutions to work around the problem, but the more obvious one would be to use the createdb command that comes with your PostgreSQL installation instead of the one that is preinstalled in your system.

Should you need further help with that, you should specify how you installed PostgreSQL, because there are quite a few different packages that provide it, and they all use different paths to avoid trampling over each other.

Update for Homebrew

If using brew, I suggest to apply the answer on superuser.com: How should I set the PATH variable on my Mac so the Hombrew-installed tools are found?

Presumably the author of the Postgres+Postgis tutorial you're referring to has changed its PATH long ago and forgot to mention it in his text.

Once done, the command which createdb should return /usr/local/bin/psql which is the homebrew version instead of the initial /usr/bin/psql which is the Apple version.