Setting up PostgreSQL on mac

https://app.dataquest.io/m/250/project%3A-postgresql-installation/2/installing-postgresql

Add the following line to the end of ~/.bash_profile:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

when I ran this in the terminal window, I didn’t get a response, is that what is expected?

Additionally, there are two other instructions on subsequent pages:

Page 3:

pip install psycopg2

I ran pip install psycopg2-binary and it worked, is this the same thing?
or
conda install psycopg2

Page 4:

Launch your Python shell and import the psycopg2 library.

Is Python shell the same as IDLE?

when I run this here, I get:

Traceback (most recent call last):
File “<pyshell#0>”, line 1, in
import psycopg2
ModuleNotFoundError: No module named ‘psycopg2’

Then, run the following code to connect to PostgreSQL and test that everything works as expected:

Where do I run the following code from? I attempted to run it in a Jupyter notebook, but when I query the tables notes, I get an error, Undefined table?

import psycopg2
conn = psycopg2.connect(dbname=“postgres”, user=“postgres”)
cursor = conn.cursor()
cursor.execute(“CREATE TABLE notes(id integer PRIMARY KEY, body text, title text)”)
conn.close()

I ran the import psycopg2 in terminal window and then the expected python and sql statement and was able to get back an empty list upon querying the notes database, but I still get the same “ModuleNotFoundError: No module named ‘psycopg2’” in IDLE when I run import psycopg2 there.

@molendykj you can do this with (in the mac terminal):

$ nano ~/.bash_profile

when the document opens, use the down arrow key to navigate to the bottom thereafter copy paste the syntax export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

...
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

Once you are done hit Ctrl-X followed by y then Enter. Please research online for the full details of using nano. You can use vim or other editors if you are not comfortable with nano.

no. this refers to what you should type in your mac terminal and should work assuming you checked add python to path when installing python. If you dont have it added to path, please research how to do so. The conda action is if you have anaconda installed. Python shell is idle after you hit Run or F5.

You may wish to refer to this article.

@masterryan.prof, thanks for your help on this, it looks like I have it working as expected. I was able to use nano in a couple of different ways to get the path setup and psycopg2 installed afterwards.

it’s pretty obvious at this point that I need to spend some time with command line.

1 Like

this worked for me:

2 Likes

thank you @gosaints, I appreciate the share!

1 Like