![]() # close the communication with the PostgreSQLĮxcept (Exception, psycopg2.DatabaseError) as error: # display the PostgreSQL database server version Print( 'Connecting to the PostgreSQL database.') #!/usr/bin/python import psycopg2ĭef connect (): """ Connect to the PostgreSQL database server """ The following connect() function connects to the suppliers database and prints out the PostgreSQL database version. Return db Code language: Python ( python ) Raise Exception( 'Section file'.format(section, filename)) The config() function is placed in the config.py file: #!/usr/bin/python from configparser import ConfigParserĭef config (filename= 'database.ini', section= 'postgresql'): # create a parser The following config() function read the database.ini file and returns connection parameters. gitignore file will be like this: database. gitignore file to not committing the sensitive information to the public repo like github. Notice that if you git, you need to add the database.ini to the. Password=SecurePas$1 Code language: Shell Session ( shell )īy using the database.ini, you can change the PostgreSQL connection parameters when you move the code to the production environment without modifying the code. The following shows the contents of the database.ini file: ![]() To make it more convenient, you can use a configuration file to store all connection parameters. port: the port number that defaults to 5432 if it is not provided.host: database server address e.g., localhost or an IP address.password: password used to authenticate.user: the username used to authenticate.database: the name of the database that you want to connect.The following is the list of the connection parameters: Password= "Abcd1234") Code language: Python ( python ) ![]() Or you can use a list of keyword arguments: conn = nnect( To call the connect() function, you specify the PostgreSQL database parameters as a connection string and pass it to the function like this: conn = nnect("dbname=suppliers user=postgres password=postgres") Code language: SQL (Structured Query Language) ( sql ) By using the connection object, you can create a new cursor to execute any SQL statements. The connect() function creates a new database session and returns a new instance of the connection class. To connect to the suppliers database, you use the connect() function of the psycopg2 module. CREATE DATABASE suppliers Code language: SQL (Structured Query Language) ( sql ) Connect to the PostgreSQL database using the psycopg2 Second, use the following statement to create a new database named suppliers in the PostgreSQL database server. Sudo python setup.py install Code language: Shell Session ( shell ) Create a new databaseįirst, log in to the PostgreSQL database server using any client tool such as pgAdmin or psql. If you have downloaded the source package into your computer, you can use the setup.py as follows: python setup.py build Second, use the following command line from the terminal: pip install psycopg2 Code language: Shell Session ( shell ) Summary: in this tutorial, you will learn how to connect to the PostgreSQL database server in the Python program using the psycopg database adapter.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |