The psycopg2 module content — Psycopg 2.9.3 documentation
www.psycopg.org › docs › moduleThe connection parameters can be specified as a libpq connection string using the dsn parameter: conn = psycopg2.connect("dbname=test user=postgres password=secret") or using a set of keyword arguments: conn = psycopg2.connect(dbname="test", user="postgres", password="secret") or using a mix of both: if the same parameter name is specified in both sources, the kwargs value will have precedence over the dsn value.
Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]
pynative.com › python-postgresql-tutorialMar 09, 2021 · import psycopg2 try: connection = psycopg2.connect(user="postgres", password="pynative@#29", host="127.0.0.1", port="5432", database="postgres_db") cursor = connection.cursor() # Executing a SQL query to insert data into table insert_query = """ INSERT INTO mobile (ID, MODEL, PRICE) VALUES (1, 'Iphone12', 1100)""" cursor.execute(insert_query) connection.commit() print("1 Record inserted successfully") # Fetch result cursor.execute("SELECT * from mobile") record = cursor.fetchall() print ...
psycopg2 · PyPI
https://pypi.org/project/psycopg229.12.2021 · If prerequisites are met, you can install psycopg like any other Python package, using pip to download it from PyPI: $ pip install psycopg2. or using setup.py if you have downloaded the source package locally: $ python setup.py build $ sudo python setup.py install. You can also obtain a stand-alone package, not requiring a compiler or external ...
The connection class — Psycopg 2.9.3 documentation
www.psycopg.org › docs › connectionThe connection class¶ class connection¶ Handles the connection to a PostgreSQL database instance. It encapsulates a database session. Connections are created using the factory function connect(). Connections are thread safe and can be shared among many threads. See Thread and process safety for details. Connections can be used as context ...
python - Making sure that psycopg2 database connection alive ...
stackoverflow.com › questions › 1281875import psycopg2 import subprocess connection = psycopg2.connect( dbname=database, user=username, password=password, host=host, port=port ) print connection.closed # 0 # restart the db externally subprocess.check_call("sudo /etc/init.d/postgresql restart", shell=True) # this query will fail because the db is no longer connected try: cur = connection.cursor() cur.execute('SELECT 1') except psycopg2.OperationalError: pass print connection.closed # 2
Python PostgreSQL with psycopg2 module
https://zetcode.com/python/psycopg206.07.2020 · import psycopg2 The psycopg2 is a Python module which is used to work with the PostgreSQL database. con = None We initialize the con variable to None. In case we could not create a connection to the database (for example the disk is full), we would not have a connection variable defined. This would lead to an error in the finally clause.