1

PostgreSQL accessed by Python in AWS Lambda with psycopg2.
This function:

cur.execute("INSERT INTO table (column1, column2, column3) VALUES ('test1', 'test2', 'test3');")

Does not add any row to the database. Psycopg2 does not throw an error. PostgreSQL logs the statement without an error. HOWEVER! There is a fourth column, which is an auto-incremented id. The sequence used to keep track of the id is increased by one, even though the row isn't added. (Weird already)

If the same statement is sent in any other way (command line, pgAdmin, Adminer), it works.

I have no single idea what is happening.

EDIT: full code:

def db():
    with open('configEC2.json') as json_data_file:
        POSTGRES = json.load(json_data_file)
    db = psycopg2.connect( host=POSTGRES["sql"]["host"], user=POSTGRES["sql"]["user"], password=POSTGRES["sql"]["passwd"], dbname=POSTGRES["sql"]["db"] )
    return db
def dbSetup():
    dbR = db()
    cur = dbR.cursor()
    return cur

cur = dbSetup()
cur.execute("INSERT INTO table (column1, column2, column3) VALUES ('test1', 'test2', 'test3');")
cur.close()
7
  • 1
    quick question, is the query supposed to be like that or did you just use a sample query to show how its working? also can you show the whole part of your code, you might be forgetting to do a commit once you execute the query. Commented May 10, 2018 at 19:09
  • The query is exactly the same, just different names. Commented May 10, 2018 at 19:11
  • ok, would you mind adding the later part of your code? you're probably missing the commit which would explain why you're not getting an error and the record is not getting created. Commented May 10, 2018 at 19:12
  • @HonzaNijaký We need more code, just that single line has nothing wrong with it so we need more code to diagnose the problem. We should see the connect (minus sensitive info) through the closing of the cursor. Commented May 10, 2018 at 19:14
  • 1
    Working on it... Commented May 10, 2018 at 19:17

1 Answer 1

5

Seems like you're not doing a commit which isn't saving your code, so I suggest doing:

def db():
    with open('configEC2.json') as json_data_file:
        POSTGRES = json.load(json_data_file)
    db = psycopg2.connect( host=POSTGRES["sql"]["host"], user=POSTGRES["sql"]["user"], password=POSTGRES["sql"]["passwd"], dbname=POSTGRES["sql"]["db"] )
    return db

conn = db()

cur = conn.cursor()

cur.execute("INSERT INTO table (column1, column2, column3) VALUES ('test1', 'test2', 'test3');")

conn.commit()

Note the conn.commit() since it's required to commit your insert into the database. I've made this mistake before and it was annoying to debug.

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

3 Comments

Thanks, works. I thought you were supposed to do cur.commit(), but it's conn.commit().
yeah, it's somewhat confusing when you've not seen the documentation. Got it wrong before as well! Cheers
You should also be closing your cursor (and connection if you don't need to keep the connection open). In general it should go connect(), cursor(), execute(), commit(), cursor.close(), connection.close()

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.