0

I've created a simple python app using heroku with django that writes to a Postgres database online. I also want to read and write to this database from a raspberry pi program using Python. Is it possible to do so without running heroku or django on the pi? Is there a simple way of accessing the database?

2
  • Welcome to Stack Overflow. Unfortunately, your question isn't entirely clear. Are you asking how to connect to an arbitrary PostgreSQL database from plain Python, or specifically how to connect to a Heroku Postgres database from a non-Heroku server? Commented Jun 3, 2019 at 12:14
  • @Chris 1. A simple app, created with django that allows a user to read and write to a local sqlite database. 2. Modified settings.py to to configure Postgresql database on heroku: import dj_database_url DATABASES = { 'default': dj_database_url.config(default='postgres://localhost') } 3. Pushed to heroku server: Git push heroku master I now want to access this postgres database from the pi in the simplest way possible using python. The pi is controlling hardware and doesn’t need any user interface. Commented Jun 4, 2019 at 8:20

1 Answer 1

2

First you need adapter library installed. You can use psycopg2. You can install it by:

pip install psycopg2

After this is done, you can create connection and cursor object in python:

import psycopg2
conn = psycopg2.connect("dbname=YOUR_DB_NAME user=USER password=PASSWORD")
cur = conn.cursor()

Then you can exectute your SQL query on the connection by:

cur.execute(YOUR_SQL, ITERABLE_WITH_VALUES_TO_ESCAPE)
conn.commit()

In the end you should close cursor and connection

cur.close()
conn.close()

Please note that this is minimal code without any error handling and with some assumptions taken. Look here for further information:

http://initd.org/psycopg/docs/install.html

http://www.postgresqltutorial.com/postgresql-python/connect/

https://opensource.com/article/17/10/set-postgres-database-your-raspberry-pi

Sign up to request clarification or add additional context in comments.

4 Comments

I think this question is specifically about accessing an existing Heroku Postgres database.
@Chris the psycopg2.connect should allow for a database_url parameter stackoverflow.com/a/52776219/4872140 Intalling the binary is probably preferred pip install pyscopg2-binary on your pi.
@Chris I interpreted question part "Is it possible to do so without running Heroku or Django on the pi?" as a request to keep Django and Heroku out of the stack. But I admit that alternative interpretation is possible. It is hard to know for sure without knowing what the app is and does.
Agreed, @Jan. Unfortunately I ended up voting to close this question as unclear. Hopefully OP comes back to improve it so I can retract my vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.