问题
I'm trying to setup postgres on OSX. I was having problems with a rails app ('cannot connect to database') and reinstalled postgres with homebrew. I cannot issue postgres commands from terminal now though:
$ sudo -u _postgres psql
Password:
psql: FATAL: role "_postgres" does not exist
$ brew list
autoconf libksba openssl python sqlite
automake libtool ossp-uuid rbenv
gdbm libyaml pkg-config readline
libgpg-error mongodb postgresql ruby-build
$ postgresql
zsh: command not found: postgresql
$ psql
psql: FATAL: database "connorleech" does not exist
This command does work though:
$ sudo -u _postgres
usage: sudo -h | -K | -k | -L | -V
usage: sudo -v [-AknS] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U user name] [-u
user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
name|#uid] file ...
After I reinstalled postgresql with homebrew my rails app began working with this app/config/database.yml:
development:
adapter: postgresql
encoding: utf8
host: localhost
database: project_development
pool: 5
username:
password:
Questions:
- How do I issue
postgrescommands from the terminal? - How is rails running an app if I never created a local
postgresuser or database?
回答1:
From Homebrew FAQ:
Homebrew is designed to work without using sudo. You can decide to use it but we strongly recommend not to do so. If you have used sudo and run into a bug then it is likely to be the cause...
Your first idea of using sudo -u _postgres psql does not apply to homebrew. I believe it applies to PostgreSQL as packaged by Apple (which ships with OS X Server Edition, not with the desktop version)
The brew postgresql package doesn't need or create users named _postgres or postgres, it suggests that you run the server under your own OS user.
/usr/local/bin/psql alone should successfully launch and connect to your default database if the server is running and there's no other configuration problem.
See also the output of brew info postgresql, it gives the required bits of information to start.
回答2:
If you installed postgres from here for Mac OSX try this:
- From root in bash "psql"
- This will log on in as the user that the app created (in my case this was my mac user name e.g. "jamesbrown")
- you'll be at a prompt like jamesbrown=#... enter "\du"
- you'll now see all the roles, mine included jamesbrown and _postgres
- if you want to add superuser to _postgres, "alter user _postgres superuser;"
来源:https://stackoverflow.com/questions/25558393/postgres-no-users-or-roles-cant-create-but-works-with-rails