Search code examples
postgresqlmacoscommand-linepsqlpgadmin

How do I interact with PostgreSQL using CLI on a Mac?


I've been using pgAdmin with PostgreSQL just fine but I wanted to get some practice using the command line. When I installed PostgreSQL, I made sure to include Command Line Tools. I downloaded it directly from Postgres' website - I did not use Homebrew.

From Terminal, I tried connecting using psql but it didn't work. So, I tried to see if I had set the path correctly as mentioned here: https://dba.stackexchange.com/questions/93369/postgres-installation-on-macosx-problem-with-psql

I used mdfind psql to find the path: PATH="/Library/PostgreSQL/13/bin:${PATH}"

I reloaded the .zprofile, entered psql and my password for my current user (I also tried the PostgreSQL password) but I got the following error:

psql: error: could not connect to server: FATAL: password authentication failed for user "me"

I also tried changing users to "postgres" via: sudo su - postgres and entered in the same passwords that I previously tried but it keeps saying "Sorry, try again" and then exits after 3 attempts.

pgAdmin4 is working fine, as usual. The passwords I enter into pgAdmin are the same that I tried using Terminal. Does anyone know what I am doing wrong?

Edit: So, it turns out that I just needed to write sudo su postgres without the -. I then entered my user password, wrote psql, entered the password and then I was able to interact with Postgres using the command line. If you're having difficulty as I was, try this and set the path as I did above.


Solution

  • So, it turns out that I just needed to write sudo su postgres without the -. I then entered my user password, wrote psql, entered the password and then I was able to interact with Postgres using the command line.

    If you're having difficulty as I was, try this and set the path as I did in my original post.