9

brew install postgresql

Initialize...

[~] initdb   
The files belonging to this database system will be owned by user "pma".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  C
  CTYPE:    UTF-8
  MESSAGES: C
  MONETARY: C
  NUMERIC:  C
  TIME:     C
The default database encoding has accordingly been set to UTF8.
initdb: could not find suitable text search configuration for locale UTF-8
The default text search configuration will be set to "simple".

creating directory /Users/pma/.pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers ... 1600kB
creating configuration files ... ok
creating template1 database in /Users/pma/.pgdata/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

    postgres -D /Users/pma/.pgdata
or
    pg_ctl -D /Users/pma/.pgdata -l logfile start

Start...

[~] pg_ctl start
server starting
[~] LOG:  database system was shut down at 2011-11-30 22:41:57 HKT
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

So far so good... Createdb....

[~] createdb test 
createdb: could not connect to database postgres: could not connect to server: Permission denied
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

... now what?

fivetwentysix
  • 7,379
  • 9
  • 40
  • 58
  • So, what are your permissions on `/var/pgsql_socket/` and also on `/var/pgsql_socket/.s.PGSQL.5432`? It does say "permission denied" in the error message. – derobert Nov 30 '11 at 14:47
  • Gives me the same error if I stop the server, chmod 777 /var/pgsql_socket, start the server – fivetwentysix Nov 30 '11 at 14:52
  • ... and the file? If that doesn't turn out to help, also check the pg_hba.conf to see if your user is authorized. And also you could try strace to see what the permission denied is coming from. – derobert Nov 30 '11 at 14:53
  • [~] chmod 777 /var/pgsql_socket/.s.PGSQL.5432 chmod: /var/pgsql_socket/.s.PGSQL.5432: No such file or directory – fivetwentysix Nov 30 '11 at 14:55
  • Also check `ps` to make sure you actually have postgres processes running. – derobert Nov 30 '11 at 15:00

3 Answers3

18

Could be the unixdomain socket is located somewhere else, eg /tmp

Try using a -h flag with either localhost or /tmp :

  • createdb -h localhost test, or
  • createdb -h /tmp test

(the -h / --hostname flag normally takes a hostname as argument, but it will accept a directoryname too, specifying the location of the unix-domain socket)

link to related topic

Community
  • 1
  • 1
wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • 1
    This diagnosed the problem, which is great. In other words I can do a `psql -h \var\pgsql_socket` but `psql` on its own gives the OP's original error. But it doesn't fix my problem with e.g. `rake db:migrate` in Ruby on Rails which launches `psql` internally and fails for this reason. How do I fix it so `psql` runs correctly with no arguments? – Dominic Sayers Dec 10 '12 at 15:14
  • 1
    @DominicSayers: this issue has come up more than once. In another question with the same problem somebody added a relevant change to the rails configuration file. See if I can dig that up ... There she is: http://stackoverflow.com/a/8482546/905902 – wildplasser Dec 10 '12 at 23:09
  • thanks, that helped me fix it. For me it started to happen when I upgraded to PostgreSQL 9.2.2. Before that (9.2.1) it was OK. – Dominic Sayers Dec 12 '12 at 18:14
2

Note for installing postgres with brew on mountain lion in June 2015: plsq and createdb are looking for the socket in /var/pgsql_socket, but postgres is creating it in /tmp. "plsq -h localhost" works fine, but to make a permanent fix:

sudo mkdir /var/pgsql_socket sudo chown <user> /var/pgsql_socket

Then edit /usr/local/var/postgres/postgresql.conf, and set unix_socket_directories = '/var/pgsql_socket'

Tao Starbow
  • 361
  • 3
  • 10
0

Do you have the right permissions for you user to create a database? If not you can create/alter user by:

Create user

> su postgres
> createuser
    ... Set user permission.

Also check your pg_hba.conf that you have the right permissions for your user.

heldt
  • 4,166
  • 7
  • 39
  • 67