0

I am using psycopg2 in python to manage my database.

I created two tables. One called data that I want to populate with some data. For doing so I created a temporary table called temporarytable from a .csv file.

I want to copy the column numberofinhabitants. So what I am doing is the following:

### Add Column
import psycopg2 as ps    
stat1 = """  ALTER TABLE data ADD COLUMN numberofinhabitants integer"""
con = ps.connect(dbname = 'mydb', user='postgres', host='localhost', password='mypd')
con.autocommit = True
cur = con.cursor()
cur.execute(stat1)
cur.close()
con.close()


### Copy Column
stat2 = """INSERT INTO data (numberofinhabitants) SELECT numberofinhabitants FROM temporarytable"""
con = ps.connect(dbname = 'mydb', user='postgres', host='localhost', password='mypd')
con.autocommit = True
cur = con.cursor()
cur.execute(stat2)
cur.close()
con.close()

but I get the following error

ProgrammingError: column "numberofinhabitants" does not exist
LINE 1: INSERT INTO data (numberofinhabitants) SELECT numberofinhabi...
                                                      ^
HINT:  There is a column named "numberofinhabitants" in table "data", but it cannot be referenced from this part of the query.

Below a screenshot from pgAdmin3 after SELECT * FROM temporarytable;

enter image description here

6
  • Where are you creating temporarytable? Can't it be that the table, being temporary, is already deleted by the time you try to copy? Commented Sep 14, 2017 at 14:10
  • Maybe you shouldn't close the connection and reopen it (that may remove the temp table) or you should create a regular table. Commented Sep 14, 2017 at 14:11
  • @franciscosollima it is a regular table that I called temporarytable Commented Sep 14, 2017 at 14:13
  • Oh nevermind then Commented Sep 14, 2017 at 14:14
  • Can you show the output of SELECT * FROM temporarytable? Commented Sep 14, 2017 at 14:16

1 Answer 1

1

I think the problem is that PostgreSQL's columns are case sensitive. You should try this as stat2:

stat2 = """INSERT INTO data (numberofinhabitants) SELECT "numberOfInhabitants" FROM temporarytable"""

Note that you should also use " for columns with upper characters in them.

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

Comments

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.