*After deploying my django model in heroku and migrations,
I am unable to load data into the postgres table by batch run
*
Ex:
heroku run python ProddbUpload.py
I am disappointed with below error
Running python ProddbUpload.py
on ⬢ spellbeeword... up, run.9508 (Free) relation "spellbeeword_tb" does not exist
Verified table name Spellbeeword_tb exist in my Postgres database in heroku environment using pg:psql My code to upload data into above mentioned table
import os import psycopg2 def writedb(conn): cur = conn.cursor() try: with open("spellbee/docs/Spell_Bee_Word_db.csv", 'r') as f: cur.copy_from(f,'Spellbeeword_tb',sep=',') # commit changes conn.commit() except Exception as error: print(error) finally: if conn: conn.close() f.close() def main(): DATABASE_URL = os.environ['DATABASE_URL'] try: conn = psycopg2.connect(DATABASE_URL, sslmode='require') writedb(conn) except(Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn: conn.close() if __name__ == "__main__" : main()
In pg:psql query, it shows database connection for my table as --> Connecting to postgresql-flat-40316
But in heroku environment variables , it is showing as
=== spellbeeword Config Vars
DATABASE_URL: postgres://mlsudzmqspljyc:c2e68f055e685aa45dde87@ec2-18-206-20-102.compute-1.amazonaws.com: By default the above config made.
I dont know how to connect postgresql-flat-40316/Spellbeeword_tb
when I checked with the below code, nothing printed
cur.execute('SELECT version()')
display the PostgreSQL database server version
db_version = cur.fetchone() print(db_version)
Pls help me to import my data into table created by django model in heroku.
I think , in heroku run it is not identifying the correct database to connect .
I followed heroku postgresl django deployment documentation where
DATABASE_URL were configured by dj-database-url installation
https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-python
I ran the above dbupload code via heroku run python <dbupload-filename.py>
requirements and settings.py were pushed to heroku before this run seperately
没有评论:
发表评论