Connecting to Postgres database on Heroku


#1

How can I connect to my Postgres database on Heroku?
I use Python 3.5
Thank you!


#2

Install psycopg2:
pip install psycopg2 pip freeze > requirements.txt
Then, of course, you need to add this line in the package section of your code:
import psycopg2
You will also need to add these:
import os
import urlparse

Report the database URL in your code:
urlparse.uses_netloc.append(“postgres”)
url = urlparse.urlparse(os.environ[“YOUR_DATABASE_URL_HERE”])

Add the connect function:
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)

And you should be good to go.


#3

I write this code. But I have mistake
Traceback (most recent call last):
File “/home/ubuntu/workspace/luchok.py”, line 11, in
import urlparse
ImportError: No module named ‘urlparse’

I use Python 3.5.2


#4

Yes, did you try running $ pip install urlparse?


#5

(venv) v_ladimir:~/workspace (master) pip install urlparse Collecting urlparse Could not find a version that satisfies the requirement urlparse (from versions: ) No matching distribution found for urlparse (venv) v_ladimir:~/workspace (master)


#6

I made a mistake. Here are some python packages you can install for urlparse:
urlparse3 - urlparse3 is simple and powerful url parsing tool
urlparse2 - urlparse for humans.
urlparse4 - Performance-focused replacement for Python’s urlparse module

Source: Python package list
I verified that these packages are still in existence.


#7

I install urlparse3 and urlparse2.

(venv) v_ladimir:~/workspace (master) pip install urlparse3 Collecting urlparse3 Downloading urlparse3-1.0.10.tar.gz Building wheels for collected packages: urlparse3 Running setup.py bdist_wheel for urlparse3 ... done Stored in directory: /home/ubuntu/.cache/pip/wheels/ea/ab/4f/f110d0502853d96542af8f2a770c591197d57445a06782ef85 Successfully built urlparse3 Installing collected packages: urlparse3 Successfully installed urlparse3-1.0.10 (venv) v_ladimir:~/workspace (master) pip install urlparse2
Collecting urlparse2
Downloading urlparse2-1.1.1.tar.gz
Collecting recordtype (from urlparse2)
Downloading recordtype-1.1.tar.gz
Building wheels for collected packages: urlparse2, recordtype
Running setup.py bdist_wheel for urlparse2 … done
Stored in directory: /home/ubuntu/.cache/pip/wheels/d0/08/0e/1bb071cd176e00fcbc1dc7aba3e0f649b4f54c0e8c3203c7ac
Running setup.py bdist_wheel for recordtype … done
Stored in directory: /home/ubuntu/.cache/pip/wheels/0e/52/54/c5f783e96e2b1f5364d360716b47768c21d84efa73d3745b45
Successfully built urlparse2 recordtype
Installing collected packages: recordtype, urlparse2
Successfully installed recordtype-1.1 urlparse2-1.1.1

but i have mistake

Important: use os.getenv(PORT, 8080) as the port and os.getenv(IP, 0.0.0.0) as the host in your scripts!

Traceback (most recent call last):
File “/home/ubuntu/workspace/luchok.py”, line 13, in
import urlparse2
ImportError: No module named ‘urlparse2’

Process exited with code: 1


#8

You should purge urlparse2, because it is not a good idea to have both running, and my suggestion would be to keep urlparse3, as it is more advanced. It is also more likely to run better on Python 3.5.

Try this fix. If it does not work, try this:

Originally, I said this:[quote=“IsaacDGoodman, post:2, topic:9991”]
urlparse.uses_netloc.append(“postgres”)
url = urlparse.urlparse(os.environ[“YOUR_DATABASE_URL_HERE”])
[/quote]However, the function urlparse.urlparse might be invalid in Python 3.5. Have you tried urllib.parse instead?
EX:
urlparse.uses_netloc.append(“postgres”)
url = urllib.parse(os.environ[“YOUR_DATABASE_URL_HERE”])

If the function urllib.parse does not work, go back to urlparse.urlparse.
That should solve your problem.


Connecting to heroku and how to code for it with c9
#9

I uninstalles urlparse2

(venv) v_ladimir:~/workspace (master) $ pip uninstall urlparse2
Uninstalling urlparse2-1.1.1:
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2-1.1.1.dist-info/DESCRIPTION.rst
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2-1.1.1.dist-info/INSTALLER
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2-1.1.1.dist-info/METADATA
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2-1.1.1.dist-info/RECORD
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2-1.1.1.dist-info/WHEEL
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2-1.1.1.dist-info/metadata.json
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2-1.1.1.dist-info/top_level.txt
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2/init.py
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2/pycache/init.cpython-34.pyc
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2/pycache/urlparse2.cpython-34.pyc
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2/urlparse1.py
/home/ubuntu/workspace/venv/lib/python3.4/site-packages/urlparse2/urlparse2.py
Proceed (y/n)? y
Successfully uninstalled urlparse2-1.1.1

But urlparse3 is also no)

Important: use os.getenv(PORT, 8080) as the port and os.getenv(IP, 0.0.0.0) as the host in your scripts!

Traceback (most recent call last):
File “/home/ubuntu/workspace/luchok.py”, line 13, in
import urlparse3
ImportError: No module named ‘urlparse3’

Process exited with code: 1


#10

Hm. That’s odd. This may not work, but try this:
Edit the file luchok.py, and change import urlparse3 to import urlparse.
If you want to do it from the terminal, run this:
$ nano luchok.py
Then, change this line:
import urlparse3
to
import urlparse
press Ctrl + X, then Y. That might fix this problem.


#12

now I import urlparse3:

import psycopg2
import os
import urlparse3

urlparse.uses_netloc.append(“postgres”)
url = urllib.parse(os.environ[“postgresql-animate-89707”])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)

but I have mistake:

Uh oh, can’t connect. Invalid dbname, user or password?
‘module’ object has no attribute 'connect’
Traceback (most recent call last):
File “/home/ubuntu/workspace/postgred.py”, line 5, in
urlparse.uses_netloc.append(“postgres”)
NameError: name ‘urlparse’ is not defined


#13

What I meant was, change import urlparse3 to import urlparse You may also want to change urllib.parse to urlparse.urlparse . Your reply was rather unclear.