1

I need to get access to pg_hba.conf to try and fix my broken postgres development db that gives the error on RAILS_ENV=development rails s of

PG::ConnectionBad
fe_sendauth: no password supplied

This post at least seems to suggest that such access may help: PG::ConnectionBad: fe_sendauth: no password supplied

The problem is, even though I know the path

/Library/PostgreSQL/9.3/data/pg_hba.conf

When I actually try to cd into it:

cd /Library/PostgreSQL/9.3/data/

I get: cd:cd:13: permission denied: /Library/PostgreSQL/9.3/data/

The seemingly obvious fix would be to sudo, so I try that:

sudo cd /Library/PostgreSQL/9.3/data/

And nothing happens. Literally the next line shows that I'm still exactly where I was. How can sudo be denied? And how can I either access this file or fix my issue?

Thanks!

Community
  • 1
  • 1
Laser
  • 5,085
  • 4
  • 34
  • 48
  • 1
    So why don't you just `sudo vim /Library/PostgreSQL/9.3/data/pg_hba.conf` instead? – Tamer Shlash Feb 08 '15 at 06:38
  • Ah I tried 'sudo subl /Library/PostgreSQL/9.3/data/pg_hba.conf' but got it to work with Vim. I didn't realize some text editors had super powers, what gives? Unfortunately I don't really know vim, but probably good to start as my understanding is that it is far more powerful than sublime text. – Laser Feb 08 '15 at 06:41
  • 1
    For Linux Administration tasks it's better to use a command line text editor ;) – Tamer Shlash Feb 08 '15 at 06:45
  • @TamerShlash This is on OS X, so it's not Linux administration at all. But yes, for *command line* admin on any platform it is usually wise to use a command line text editor like `nano` or `vi`. – Craig Ringer Feb 08 '15 at 07:25
  • @Laser The editor doesn't matter except that `vim`, `nano`, etc actually run directly as a subprocess of the invoking shell. It's not so much that `vim` has super powers as that the editor you are using is doing something different to what the great majority of editors do. – Craig Ringer Feb 08 '15 at 07:27

1 Answers1

3

This:

sudo cd /Library/PostgreSQL/9.3/data/

runs the cd command under sudo. sudo actually runs a new instance of the shell (/bin/sh or whatever) then runs the command in the shell.

The current directory is a property of the current process. It is inherited by new child processes, but it changes do not get propagated up to parent processes.

What you've done is the equivalent of:

sh -c 'cd /tmp'

It makes a new shell, cds to a location, then exits. The effect of the cd only affects that shell, So it effectively did nothing.

What you should do instead is use sudo to open the file in your text editor by absolute path, e.g.:

sudo nano /Library/PostgreSQL/9.3/data/pg_hba.conf

(nano is a simple and user-friendly command line text editor; I'm assuming you don't know how to use vi given this question.)

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • but then why does `sudo subl /Library/PostgreSQL/9.3/data/pg_hba.conf` render a blank document? Is it because its outside of the terminal window so the file editor doesn't have sudo access ad gets denied? – Laser Feb 08 '15 at 07:18
  • 1
    I assume `subl` is a wrapper for invoking the "sublime" text editor you mentined above? If so: Many programs, especially on Mac, don't launch a full new copy of the program when you run them on the command line. They just connect to the existing running copy and tell it "open this file please" then exit. The permissions of the already-running copy are what apply, not the permissions of the wrapper. A carefully written program would give you an error message clearly telling you "Permission denied" when it failed to open the file, but it sounds like Sublime just ignores the error instead... – Craig Ringer Feb 08 '15 at 07:24
  • @Laser You might find that if you *completely quit* Sublime then run `subl blahfile` it will work, because the whole editor will get run as root. But that's a bad idea, do not run a full featured GUI text editor as root. – Craig Ringer Feb 08 '15 at 07:28
  • Yes, that makes more sense since I'm not closing out the program when I run the wrapper. Thanks for the education! :) – Laser Feb 08 '15 at 07:31