Unable to Open Postgres shell in Mac Terminal ZSH - postgresql

Disclaimer that I'm both new to Mac & Postgres. I recently got the 2020 MacBook Pro and have been setting it up for my personal [learning] development computer. In the process of attempting to install Postgres, I am having issues and am not sure what I am doing wrong / missing.
I've installed Postgres via Homebrew using:
brew install postgresql
All seems to have been installed fine, but whenever I try to open up a Postgres shell to enter commands into (coming from a Windows/Bash background), I keep getting the same response:
>> psql
[1] ##### killed psql
>> psql postgres
[1] ##### killed psql postgres
>> psql -U postgres
[1] ##### killed psql -U postgres
When asking where psql is:
>> where psql
/opt/homebrew/bin/psql
When trying to make sure postgresql is launched I run as instructed in the brew installation output:
>> pg_ctl -D /opt/homebrew/var/postgres start
pg_ctl: directory "/opt/homebrew/var/postgres" is not a database cluster directory
And when attempting to check the Postgres version:
>> postgres -V
[1] ##### killed postgres -V
I am not sure what I'm missing and any help would be appreciated! I'm trying to set up Postgres so I can use it for an Angular tutorial I'm trying to understand. The backend connection has never been my strong-suit and I'm working to fix that.

I've had the same problem. Now there's a new version of Postgres that works (stable 13.3). Just upgrade the app via brew upgrade.

Related

PostgreSQL 13.3 installation on MacOS (Big Sur) failed with error: Failed to initialise the database cluster with initdb

Downloaded the PostgreSQL installer from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, and tried to install it on my new Macbook Pro running Big Sur.
The installation went fine, but it failed when executing the script /Library/PostgreSQL/13/installer/server/initcluster.sh. I traced to the following script:
su - $OSUSERNAME -c "$INSTALLDIR/bin/initdb --pwfile $INSTALLDIR/installer/server/initdbpw.$$ --encoding=utf8 -A scram-sha-256 -U \"$SUPERNAME\" -D \"$DATADIR\""
Apparently, su - postgres does not work because the new service account postgres is not enabled for login. Maybe this script should use sudo -u postgres instead of su - postgres.
I got it working by updating the service account postgres as follows, so it is enabled for login:
sudo dscl . -passwd /Users/postgres 'MyNewPassword'
sudo dscl . -read /Users/postgres
After you verify that the password of the account is no longer "*", you can uninstall and reinstall PostgreSQL, so it can initialize the db, as long as you provide the same password for postgres when you re-install it.

Cannot install postgres with brew. Get "FATAL: role "postgres" does not exist" no matter what command I try

I've tried to install postgres 13.1 on my Mac using homebrew. (Also tried versions 11 & 12 which at one time were installed on this Mac, but encountering same error with those versions now). Am using notes I took from previous installs & of course google/stackoverflow. Tried many things/many times, but always, if I run psql, createuser, createdb (any postgres command), it responds with:
FATAL: role "postgres" does not exist
Also tried just creating a postgres user on my Mac to run install from that account; no luck.
Also tried postgresql install from the enterprisedb.com site. That worked, but seems klunky & seems to rely on .sh scripts. Interestingly, got the 'role postgres does not exist' error after the edb install too, but noticed if I just entered 'postgres' twice on the command line it worked. Example:
/Library/PostgreSQL/13/bin/psql -h localhost -p 5432 -U postgres postgres
This trick doesn't work tho with the brew install.
Understand I need to create the postgres role, but I can't find a command to do this that doesn't complain that the role doesn't already exist.
Any help would be so so appreciated!
Most sites documenting postgres install with homebrew said to use commands like:
psql postgres
or
createuser postgres
Those didn't work for me, but this did:
psql -d postgres -U <myUserName>
Attaching a screenshot to try to summarize the install experience & what worked:
screenshot
Can you try:
/Library/PostgreSQL/13/bin/psql
and then:
CREATE USER postgres SUPERUSER;
?
This document solved it for me: https://enrq.me/dev/2021/01/13/fix-role-postgres-does-not-exist/
The gist: run createuser -s postgres -U <os-username>

PostgreSQL: Can't log into Postgres - sudo broken?

Hey so after I ran into somes issues I decided to uninstall Postgres and re-install it (re install a newer version btw).
I'm trying to access postgres to create a new db for my Rails app so I run su postgresor su _postgres then I am asked a password but nothing that I enter works.
So I tried sudo -u postgres psql and got
sudo: unknown user: postgres
sudo: unable to initialize policy plugin
I tried also to disable SIP (see: here)
but it didn't work either. same results.
I don't know what to do. Any idea ? I am using Mac OS High Sierra 10.13.6.
Thanks to #gordon Davidson and #wildplasser 's help, I found the solution:
sudo -u myusername psql postgres
I had this same challenge when trying to access a PostgreSQL database.
When I run the command below:
sudo -u postgres psql
I get the error below:
sudo: unknown user: postgres
sudo: unable to initialize policy plugin
The issue was that I did not have the PostgreSQL database server installed on the server. The server only had a MySQL database server set up on it.
That's all.
I hope this helps
From your EC2 node issue the following command to test if database is reachable.
/opt/gitlab/embedded/bin/psql -U YourExistingUsername -d template1 -h AWS-RDS-POSTGRES-ENDPOINT

Unix socket error postgresql 9.4

I just installed postgresql 9.4 on a brand new 15.04 ubuntu install and I'm unable to start the psql server. Every time I try to run sudo -u postgres psql, it gives me the following error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have tried every single solution posted here and on every other forum, but none have worked except one:
http://ubuntuforums.org/showthread.php?t=869080
sudo mkdir -p /usr/local/pgsql/data
sudo chown -R postgres:postgres /usr/local/pgsql/
sudo sudo su postgres
cd /usr/lib/postgresql/9.4/bin/
./initdb -D /usr/local/pgsql/data
./postgres -D /usr/local/pgsql/data
I tried changing the init.d script as stated there but that doesn't work, and the solution above only seems to work once, after that, a file locks up and I'm unable to run ./postgres -D /usr/local/pgsql/data
Can someone please help? I've tried removing, purging, reinstalling, multiple-versions, etc. Every single time I get the same error.
hanks!
T
psql is not the PostgreSQL server. It is the command-line client that connects to an already-running server.
It looks like you had no PostgreSQL server running, so there was nothing to connect to. Note that the error message says Is the server running...
As you have already noted, to start a PostgreSQL server you need to run postgres -D /path/to/datadir (or preferably something like pg_ctl -D /path/to/datadir -w start -l pgstartup.log).
Rather than doing so manually, you should have your operating system do this automatically when you start up. Most PostgreSQL installers and packages will set this up for you. Some require additional steps to enable it, which will be explained in their documentation. You haven't mentioned how you installed PostgreSQL / from where, so I can't be more specific about this.
If you use the Ubuntu PostgreSQL packages or http://apt.postgresql.org/ packages, they'll register a service for you.
Please see https://help.ubuntu.com/community/PostgreSQL

PostgreSQL client -- How To Start It?

I just installed PostgreSQL-8.4 on Ubuntu. How do I start it / its GUI, connect to a database etc? I know SQL, but can't find PostgreSQL's icon in my Ubuntu 10.04 desktop (hence, am not sure how to start it).
Postgresql has no built in gui.
to check if it is running run the following from a terminal
ps aux | grep postgres
You can use psql to access from the command line.
to install psql
aptitude install postgresql-client
then to run
psql -h dbhost -U username dbname
If you want a gui intall package pgadmin
aptitude install pgadmin3
I start postgres prompt by using the following command:
sudo -u postgres psql
I use Ubuntu 14.04
If you're a Mac user, try running this
postgres -D /usr/local/var/postgres
then do
psql
you may start by firing up a graphical client. In a terminal type : pgadmin3
You will be presented with the pgAdmin III interface. Click on the "Add a connection to a server" button (top left). In the new dialog, enter the address 127.0.0.1, a description of the server, the default database ("mydb" in the example above), your username ("postgres") and your password.
With this GUI you may start creating and managing databases, query the database, execute SQl etc.
check out - https://help.ubuntu.com/community/PostgreSQL