Skip to content

Instantly share code, notes, and snippets.

@dgierejkiewicz
Forked from pfigue/psycopg2_cheatsheet.md
Created November 18, 2021 11:33
Show Gist options
  • Save dgierejkiewicz/dec8c5a106c3324bbb024125250b45c7 to your computer and use it in GitHub Desktop.
Save dgierejkiewicz/dec8c5a106c3324bbb024125250b45c7 to your computer and use it in GitHub Desktop.

Revisions

  1. @pfigue pfigue revised this gist Feb 15, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -42,3 +42,7 @@ Source: [PostgreSQL 9.1 - SSL Support](http://www.postgresql.org/docs/9.1/static
    from psycopg2.extras import (DictCursor,)
    dict_cur = conn.cursor(cursor_factory=DictCursor)

    # Reference

    1. [Python DB API cheatsheet](https://wiki.python.org/moin/DbApiCheatSheet)

  2. @pfigue pfigue revised this gist Feb 13, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Connect and Select

    ## Connect
    ## `connect()`

    The shortest `connect`:

    @@ -25,7 +25,9 @@ A little bit longer:
    ### Valid `sslmode` values
    disable, allow, prefer, require, verify-ca, verify-full

    ## SELECT
    Source: [PostgreSQL 9.1 - SSL Support](http://www.postgresql.org/docs/9.1/static/libpq-ssl.html)

    ## `select`

    curs = psql_conn.cursor()
    params = {
    @@ -35,8 +37,6 @@ disable, allow, prefer, require, verify-ca, verify-full
    is_there_a_match = False if curs.fetchone() is None else True
    curs.close()

    Source: [PostgreSQL 9.1 - SSL Support](http://www.postgresql.org/docs/9.1/static/libpq-ssl.html)

    # Dictionary-like cursors

    from psycopg2.extras import (DictCursor,)
  3. @pfigue pfigue revised this gist Feb 13, 2015. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,12 @@
    # Connect and Select

    ## Connect

    The shortest `connect`:

    from psycopg2 import connect
    psql_conn = connect("dbname=XXX user=XXX password=XXX host=localhost sslmode=require")
    psql_conn.close()

    A little bit longer:

    @@ -17,17 +20,20 @@ A little bit longer:
    'sslmode': 'require',
    }
    psql_conn = connect(**psql_creds)
    psql_conn.close()

    ### Valid `sslmode` values
    disable, allow, prefer, require, verify-ca, verify-full

    ## SELECT

    curs = psql_conn.cursor()
    params = {
    'ID': the_id,
    }
    curs.execute('SELECT * FROM users WHERE id=%(ID)s;', params)
    is_there_a_match = False if curs.fetchone() is None else True
    curs.close()
    psql_conn.close()

    ## Valid `sslmode` values
    disable, allow, prefer, require, verify-ca, verify-full

    Source: [PostgreSQL 9.1 - SSL Support](http://www.postgresql.org/docs/9.1/static/libpq-ssl.html)

  4. @pfigue pfigue revised this gist Feb 13, 2015. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    # Connect and Select

    The shortest:
    The shortest `connect`:

    from psycopg2 import connect
    psql_conn = connect("dbname=XXX user=XXX password=XXX host=localhost sslmode=require")

    A little longer:
    A little bit longer:

    from psycopg2 import connect
    # Establish psql connection
    @@ -17,6 +17,14 @@ A little longer:
    'sslmode': 'require',
    }
    psql_conn = connect(**psql_creds)
    curs = psql_conn.cursor()
    params = {
    'ID': the_id,
    }
    curs.execute('SELECT * FROM users WHERE id=%(ID)s;', params)
    is_there_a_match = False if curs.fetchone() is None else True
    curs.close()
    psql_conn.close()

    ## Valid `sslmode` values
    disable, allow, prefer, require, verify-ca, verify-full
  5. @pfigue pfigue revised this gist Feb 13, 2015. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -14,13 +14,14 @@ A little longer:
    'user': 'XXX',
    'password': 'XXX',
    'host': 'localhost',
    'sslmode': 'disable',
    'sslmode': 'require',
    }
    psql_conn = connect(**psql_creds)

    ## Valid `sslmode` values
    disable, allow, prefer, require, verify-ca, verify-full
    Source: [PostgreSQL 9,1 - SSL Support](http://www.postgresql.org/docs/9.1/static/libpq-ssl.html)

    Source: [PostgreSQL 9.1 - SSL Support](http://www.postgresql.org/docs/9.1/static/libpq-ssl.html)

    # Dictionary-like cursors

  6. @pfigue pfigue revised this gist Feb 13, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,13 @@ A little longer:
    'user': 'XXX',
    'password': 'XXX',
    'host': 'localhost',
    'sslmode': 'disable',
    }
    psql_creds_str = ' '.join(\
    ['%s=%s' % (k, psql_creds[k]) for k in psql_creds.keys()])
    logger.debug('psql_creds_str: %s' % psql_creds_str)
    psql_conn = connect(psql_creds_str)
    psql_conn = connect(**psql_creds)

    ## Valid `sslmode` values
    disable, allow, prefer, require, verify-ca, verify-full
    Source: [PostgreSQL 9,1 - SSL Support](http://www.postgresql.org/docs/9.1/static/libpq-ssl.html)

    # Dictionary-like cursors

  7. @pfigue pfigue revised this gist Feb 13, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,6 @@ A little longer:

    # Dictionary-like cursors

    from psycopg2.extras import (DictCursor,)
    dict_cur = conn.cursor(cursor_factory=DictCursor)
    from psycopg2.extras import (DictCursor,)
    dict_cur = conn.cursor(cursor_factory=DictCursor)

  8. @pfigue pfigue created this gist Feb 13, 2015.
    27 changes: 27 additions & 0 deletions psycopg2_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # Connect and Select

    The shortest:

    from psycopg2 import connect
    psql_conn = connect("dbname=XXX user=XXX password=XXX host=localhost sslmode=require")

    A little longer:

    from psycopg2 import connect
    # Establish psql connection
    psql_creds = {
    'dbname': 'XXX',
    'user': 'XXX',
    'password': 'XXX',
    'host': 'localhost',
    }
    psql_creds_str = ' '.join(\
    ['%s=%s' % (k, psql_creds[k]) for k in psql_creds.keys()])
    logger.debug('psql_creds_str: %s' % psql_creds_str)
    psql_conn = connect(psql_creds_str)

    # Dictionary-like cursors

    from psycopg2.extras import (DictCursor,)
    dict_cur = conn.cursor(cursor_factory=DictCursor)