[Note] Get data from PostgreSQL with Python

It is troublesome to check it every time, so I will write it down.

import psycopg2


#Change arbitrarily
postgre_user_name = "user_name"
postgre_user_password = "user_password"
postgre_server = "server"
postgre_server_port = "server_port"
postgre_database_name = "database_name"
postgre_table_name = "table_name"

conn = psycopg2.connect("postgresql://"
                        + postgre_user_name
                        + ":" 
                        + postgre_user_password
                        + "@"
                        + postgre_server
                        + ":"
                        + postgre_server_port 
                        + "/" 
                        + postgre_database_name)
cur = conn.cursor()
sql_sentence = "select count(*) from " + postgre_table_name + ";"
cur.execute(sql_sentence)
cur.fetchall()

The connection destination is converted as follows

postgresql://user_name:user_password@server:server_port/database_name

The sql statement is converted as follows.

select count(*) from table_name;

Recommended Posts

[Note] Get data from PostgreSQL with Python
Get Youtube data with python
Get data from database via ODBC with Python (Access)
Get data from Quandl in Python
[Python] Get economic data with DataReader
Get data from analytics API with Google API Client for python
Get additional data in LDAP with python
Receive textual data from mysql with python
Get html from element with Python selenium
Use PostgreSQL data type (jsonb) from Python
Get data from Cloudant with Bluemix flask
Get data from an oscilloscope with pyVISA
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Data analysis with python 2
Get date with python
Data analysis with Python
Get stock price data with Quandl API [Python]
I tried to get CloudWatch data with Python
Python script to get note information with REAPER
Extract data from a web page with Python
Sample data created with python
Get Amazon RDS (PostgreSQL) data using SQL with pandas
How to scrape image data from flickr with python
Get Twitter timeline with python
Get financial data with python (then a little tinkering)
Get schedule from Garoon SOAP API with Python + Zeep
Reading Note: An Introduction to Data Analysis with Python
Get data from GPS module at 10Hz in Python
[Note] Operate MongoDB with Python
Get thread ID with python
Get mail from Gmail and label it with Python3
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Get home directory with python
Get keyboard events with python
[Basics of data science] Collecting data from RSS with python
With skype, notify with skype from python!
Get Alembic information with Python
Read json data with python
A memo that reads data from dashDB with Python & Spark
Notes on importing data from MySQL or CSV with Python
Get rid of dirty data with Python and regular expressions
Xpath summary when extracting data from websites with Python Scrapy
Hit REST in Python to get data from New Relic
Get additional data to LDAP with python (Writer and Reader)
Get message from first offset with kafka consumer in python
[Introduction to Python] How to get data with the listdir function
Get Leap Motion data in Python.
Python: Exclude tags from html data
Get started with Python! ~ ① Environment construction ~
Call C from Python with DragonFFI
Link to get started with python
Hit treasure data from Python Pandas
Using Rstan from Python with PypeR
Get reviews with python googlemap api
Install Python from source with Ansible
Create folders from '01' to '12' with python
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
Get one column from DataFrame with DataFrame