3

I'm trying to follow this tutorial, but at the beginning it says to run a postgresql instance "locally" instead of as a daemon using the following commands.

$ initdb pg
$ postgres -D pg &
$ createdb shouter

So I've got a new 12.04 install (VMWare on Win7, if that matters) and did under my login:

$ sudo apt-get install postgresql
$ initdb pg
$ postgres -D pg

This gives me an error saying:

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.
WARNING:  could not create listen socket for "localhost"
FATAL:  could not create any TCP/IP sockets

I figure this means installing the server caused it to run the daemon automatically. I'd prefer just running on the default port like in the tutorial, but I tried running a different port anyway:

$ postgres -D pg -p 5555
FATAL:  could not create lock file "/var/run/postgresql/.s.PGSQL.5555.lock": Permission denied

I've followed various other examples on different ways to install postgresql but all to no avail. How do I get past this error so that postgres -D pg can execute successfully?

*note I'm perfectly willing to create a brand new VM, so if you've got instructions on how to do this with a fresh install, that'd be terrific.

2 Answers 2

3

So the first thing is to stop the server. That's done as follows:

sudo /etc/init.d/postgresql stop

That was easy enough. So the rest is permissions, so apparently chmod is the solution in step 3 below. (Newbies google chmod for some insight). You have to allow write access to all users to the "socket directory". Apparently that's just a Debian problem; they modified the postgresql source code in their repo; the "socket directory" in the unmodified postgres source is "/tmp", which has by default free write permissions. However the Debian distro changed that to "/var/run/postgresql", which is readonly for non-owners. So you just have to make that writeable. Here's the full pre-tutorial install script from a virgin 12.04 Ubuntu VM.

sudo apt-get install postgresql
sudo /etc/init.d/postgresql stop
sudo chmod a+w /var/run/postgresql
echo 'PATH=$PATH:/usr/lib/postgresql/9.1/bin' >> .bashrc
. .bashrc

sudo apt-get install leiningen

mkdir clojure
cd clojure
lein new shouter
cd shouter
gedit project.clj
  change to https://devcenter.heroku.com/articles/clojure-web-application

initdb pg
postgres -D pg &
createdb shouter
export DATABASE_URL=postgresql://localhost:5432/shouter
lein repl

To keep it from auto-starting on port 5432: Edit the /etc/postgresql/9.1/main/start.conf file.

1
  • 1
    What a great answer
    – tjb1982
    Oct 12, 2012 at 0:00
2

Assuming you don't have admin access on the computer, the solution is to run Postgres with a new configuration file and tell it to create the unix socket in a directory where you already have write permission.

Initialize a new data directory, here named db.

initdb -D db

Edit configuration file db/postgresql.conf. Find the line that specifies the unix_socket_directories and change it for example to the current directory (that would be the data directory named db, not the directory from which you are running postgres)

unix_socket_directories='.'

You can now run Postgres with

postgres -D db

and create a first database, giving an absolute filepath to the unis socket in the -h option

createdb -h `pwd`/. customers

You may also want to change the default port, either in the config, or by running postgres with something like -p 5555

The connection description in your program is then going to be for example

(def psql
  {:subprotocol "postgresql"
   :subname "//localhost/shouter"})

If you changed the port from the default 5433, you have to specify it there, something like "//localhost:5555/shouter".

As a side note, the JDBC driver does not support connecting to the database via Unix sockets, only via TCP/IP sockets.

source: http://www.postgresql.org/docs/9.4/static/config-setting.html#CONFIG-SETTING-CONFIGURATION-FILE

3
  • 1
    If valid, I think this is the better answer, but I moved everything to mongodb a while back, so likely no chance to test this in the near future. If someone else can vouch for it I'll mark it as correct.
    – Dax Fohl
    Oct 22, 2014 at 2:29
  • The setting is called unix_socket_directories since PostgreSQL 9.3 (multiple directories can be specified now).
    – user686249
    Jul 15, 2015 at 18:06
  • thanks, updated. also the location of the postgresql.conf config is now in the data dir
    – user7610
    Jan 13, 2016 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .