Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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

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.

share|improve this answer
What a great answer – tjb1982 Oct 12 '12 at 0:00

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)


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.


share|improve this answer
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 '14 at 2:29
The setting is called unix_socket_directories since PostgreSQL 9.3 (multiple directories can be specified now). – user686249 Jul 15 '15 at 18:06
thanks, updated. also the location of the postgresql.conf config is now in the data dir – user7610 Jan 13 at 21:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.