title: I'm getting cursor = connection.cursor() NameError: name 'connection' is not defined error
body: <p>Below is my code</p>
  
  <pre><code>
  psycopg2.connect('dbname=example user=user host=localhost password=pass')
  
  cursor = connection.cursor()
  
  cursor.execute('''
      CREATE TABLE table2 (
          id INTEGER PRIMARY KEY,
          completed BOOLEAN NOT NULL DEFAULT False
      );
   ''')
  
  cursor.execute('INSERT INTO table2 (id, completed) VALUES (1, true);')
  
  connection.commit()
  
  connection.close()
  cursor.close()
  </code></pre>
  
  <p>And here's the error I'm getting:</p>
  
  <p><strong>Traceback (most recent call last):
    File "demo.py", line 5, in 
      cursor = connection.cursor()
  NameError: name 'connection' is not defined</strong></p>
  
  <p>I have tried everything I could find and nothing works. Any ideas on what could be causing this?</p>