*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:[email protected]: 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
# setting DATABASE url
DATABASES = {'default' : dj_database_url.config() }
=== DATABASE_URL
Plan: Hobby-dev
Status: Available
Connections: 0/20
PG Version: 13.2
Created: 2021-04-06 10:54 UTC
Data Size: 9.0 MB
Tables: 13
Rows: 68/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Continuous Protection: Off
Add-on: postgresql-flat-40316
table row: ('d7uobuibp87kvk', 'public', 'Spellbeeword_tb', 'BASE TABLE', None, None, None, None, None, 'YES', 'NO', None)
inside database ,my target table exist but it shows
relation "spellbeeword_tb" does not exist
LINE 1: SELECT * FROM Spellbeeword_tb;
^
Can anyone suggest how to speccify table name in the code to recognize