PostgreSQL on Mac OSX - cannot log in as postgres user - macos

I installed postgresql and geodjango on Mac OSX following this tutorial word for word.
For some reason
sudo su - postgres
does not work. I put in my sudo password, but when I type "whoami" it's still my normal user login.
I know there is postgres user after I saw the list from
dscl . -list /Users UniqueID
Any idea why this is happening? I installed PSQL 9.1.1 server version.
Thank you.

The postgres account probably has disabled login rights and/or /bin/false as the login shell.
In this case you had best invoke postgresql commands directly, eg: sudo -u postgres psql
That should work even with a disabled login shell.

Related

"sudo: unknown user: postgres" when trying to create postgres user

I'm using Mac 10.9.5 and PostgreSQL 9.3.4. I'm following instructions for installing a third-party software package (Instructure Canvas) on my local machine) and the instructions say to execute:
sudo -u postgres createuser $USER
sudo -u postgres psql -c "alter user $USER with superuser" postgres
but upon executing the first command, I get the error
davea$ sudo -u postgres createuser $USER
sudo: unknown user: postgres
Other posts seemed to indicate I needed to prepend a "_" to my "postgres" user, but upon doing so, i get this error ...
davea$ sudo -u _postgres createuser $USER
createuser: could not connect to database postgres: FATAL: role "_postgres" does not exist
Any ideas what I need to do to get this to work properly?
It looks like whatever you installed has a system user named _postgres and a PostgreSQL user named postgres. Fun!
Try:
sudo -u _postgres createuser -U postgres myusername
(The numerous messy, conflicting and incompatible PostgreSQL installs used on OS X are a nightmare, argh).

Unable to uninstall postgres completely from mac osx

I have completely unintsalled postgres and postgis from my system but still I am able to enter postgres through psql command . I need to uninstall completely because it is showing two psql and later I am able to reinstall afresh.
GW:~ User$ sudo -u postgres psql -h localhost
Password:
Password:
psql (9.1.5, server 9.1.14)
Type "help" for help.
postgres=#
I don't understand how is it showing 9.1.5 and 9.1.14 . I need to get rid of this. Any help would be really grateful.

What's with Postgres on OS X?

My goal is to have a Postgres setup that I can use to develop my rails app and then push it to Heroku without having to change the database config file. Seems simple enough, right?
I've seen plenty of things on the internet about PostgreSQL 9.2.x not running on OS X 10.8, and I'm still struggling to get it working with Rails. Not realizing that a version of PSQL shipped with OS X, I set out to install it. (NOTE: I'm sort of debugging in circles, so things may not have worked as I expected.)
I've tried downloading and installing using the official graphical installer listed here, and I rebooted my Mac. I saw an extra user account was created, and I promptly deleted it. (I know, I know, probably what caused some heartache down the line, but it didn't seem reasonable to have an extra user account sitting around.)
Upon running my rails app, it couldn't connect to postgres. I tried running psql from Terminal.
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?`
I then proceeded to install Postgres with homebrew. I installed homebrew, then postgres. Same thing. I googled a bit and ran into a thread on the Postgres forum. What I got from it was that something changed between OS X 10.7 and 10.8, but that wasn't the cause of my problem.
Next, I tried using self-contained the Postgres.app. Now, I was able to create users and connect to the database. Rails was able to connect to Postgres, but complained that the database that I defined in config.yaml was absent. So I created it - or so I thought.
Running CREATE DATABASE my_db in the "self-contained" version of PSQL didn't work - even though without the self contained app, PSQL would have that socket error. I then tried creating a new user with write access. Nope. The CREATE was silently failing.
I tried adding localhost to my environment variable and it seemed to have solved the port error - but now Rails was having trouble finding Postgres again.
I uninstalled the brew version. I removed the enterprise tools et al. Still no luck. Reboot. Still no luck.
I just uninstalled brew and my config looks like this:
development:
adapter: postgresql
database: my_db
pool: 5
timeout: 5000
host: localhost
port: 5432
Rails says the database doesn't exist.
What do I need to do to get Postgres working with Rails 3.2.6 on Mac OS X 10.8.4 Mountain Lion?
There are a lot of ways of getting to the point you want, but this should work:
First - get rid of any existing copies of PostgreSql as follows:
brew update
brew uninstall postgresql
(assuming no probs with brew update)
I found this removed the instance that ships with osx 10.8
If you still have the Postgres App, drag it out of Applications and into Trash
I would then do a restart, just for the hell of it... (my old Windows experience :-)
Then install the latest postgres (or u can prob specify a version here)
brew install postgres
If it throws up an error relating to something like ossd-uuid, then just do a
brew uninstall ossd-uuid && brew install ossd-uuid
and that should clear it
The homebrew lists some useful commands, eg set postgres to launch on startup etc
Create an initial postgres db by
initdb /usr/local/var/postgres -E utf8
the start postgres by
postgres -D /usr/local/var/postgres
Now at your terminal prompt, type
psql postgres
using your Mac admin password, u should get a prompt thus (\q exits):
postgres=#
A user with your username will have been created (but, from memory, not a password for it) so set that now
postgres=# alter user your-name with password 'anything';
It will return with ALTER ROLE if successful (& just the prompt if not)
Then, if your Rails database is eg dev_db, u can create a user for that thus:
postgres=# create role your_user with login createdb createrole password 'your_pass';
your_user and your_pass will appear in your rails database.yaml file (not sure if createdb & createrole NEED to be there, but thats what I did)
Finally, your database.yaml needs to have development like:
development:
adapter: postgresql
encoding: unicode
database: dev_db
pool: 5
timeout: 5000
host: localhost
username: your_user
password: your_pass
Then
rake db:create
And that should create an empty database that your Rails migrations will populate
I hope :-)

Why can't I get the postgresql server to run?

Ok. I have been trying to solve this problem for several days. I installed, uninstalled, and reinstalled Postgresql 3 times. I followed precisely the instructions in this forum: https://dba.stackexchange.com/questions/42048/cant-connect-to-the-postgres-server-ls-tmp-s-pgsql-5432-no-such-file-or-dir
I found this solution in many forums, so I tried to run:
$ mkdir /var/pgsql_socket/
$ sudo mkdir /var/pgsql_socket/
$ ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
But this didn't work. When I try to start the server it still says there is another one running and then proceeds to fail every time I try to create a database or type "psql"
I then tried to run the following in order to change the path of the commands from OS X's builtin version of postgres to my version and it seemed to work:
$ cd /usr/local/bin
$ rm postgres
$ ln -s /Library/PostgreSQL/9.2/bin/postgres postgres
$ rm psql
$ ln -s /Library/PostgreSQL/9.2/bin/psql psql
$ rm pg_ctl
$ ln -s /Library/PostgreSQL/9.2/bin/pg_ctl pg_ctl
So then I ran the following to create a user for postgres:
$ sudo -u postgres createuser --superuser $Sarah
$ sudo -u postgres createuser --superuser user_sarah
$ sudo -u postgres psql postgres
But it kept saying "unknown user postgres"
I then tried to install the Ruby pg gem, but that also failed, saying there was a problem with necessary libraries.
I have saved a text file of everything I tried to do in the terminal. Let me know if I should post that. Thanks.
update:
When I try to run this:
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
I get this:
-bash: /usr/local/bin/pg_ctl: No such file or directory
Which is different from a lot of other errors that I have seen posted on this problem.
first, you should verify that there is no postmaster running: ps -ef | grep postmaster. once you've verified the postmaster is not running, you should look into the postgresql command initd. Depending on your server installation, it may or may not be installed. You need to create a database before attempting to start postgres. It sounds to me like you got an installation that didn't install the main postgresql commands to /usr/bin, pg_ctl being one of these.
FYI: the postgres account is not a login account and is automatically created when installing postgresql, so it should be there if you had a good installation. You cannot sudo as postgres if postgres is not in the sudoers file.
The homebrew install of PostgreSQL does not create or need a postgres account, so all the mentions of doing sudo -u postgresor ps -fu postgres don't apply to your case.
The command brew info postgresql outputs various information about to start and stop it, you may read them. By contrast you don't want to put blind faith into what random users tell about how they "fixed" their non-working installation. In fact, the web carries a shocking amount of bad advice concerning PostgreSQL on MacOS X, and to me the answer you linked on dba.se is among them. It's wrong from start to finish, and you should note that it was not accepted as an answer. Certainly the author means well, but he fails to see that his own context can't be generalized to other installations.
The worst part is the suggestion to delete in /usr/local/bin/ the binaries postgres, psql, pg_ctl and soft-linking them into supposed equivalents inside /Library/PostgreSQL/9.2/bin/.
To me it just breaks the homebrew install.
No wonder that after doing this you get this error:
/usr/local/bin/pg_ctl: No such file or directory
So my answer would be to reinstall postgresql with brew to restart from a clean state, then make sure that all postgres commands you launch are from /usr/local/bin, and always first read the server log file passed to pg_ctl if you have any doubt on anything PG-related.

sudo: unknown user: postgres

I'm currently trying to run postgresql (9.3) on my iMac but I have some issues with user postgres and psql connection.
I bought an iMac old generation (with snow Leopard) that was migrated to Lion. As followed on other StackOverflow topics I added this line on my .bash_profile :
export PATH=/Library/PostgreSQL/9.3/bin:/usr/bin:$PATH
When launching pg :
sudo su postgres -c '/Library/PostgreSQL/9.3/bin/pg_ctl -D /Library/PostgreSQL/9.3/data/ -m fast start'
it returns :
su: unknown login: postgres
What should I do ?
The default postgres user in OSX 10.8 & 10.9 should be _postgres, you can use sudo -u _postgres to run the command
I got this same error (su: unknown login: postgres) when trying sudo su - postgres. I then managed to log in using sudo psql -U my.username postgres.
I could then create the appropriate users, etc.
Hope that helps.
Try to start from current user:
/Library/PostgreSQL/9.3/bin/pg_ctl -D /Library/PostgreSQL/9.3/data/ -l /usr/local/var/postgres/server.log start
Additional info: How to start PostgreSQL server on Mac OS X?

Resources