create account

Connecting to PostgreSQL database and making queries within Python code. by geekgirl

View this thread on: hive.blogpeakd.comecency.com
· @geekgirl ·
$14.47
Connecting to PostgreSQL database and making queries within Python code.
<center>![sql.png](https://images.hive.blog/DQmXYEFZ8EC3AfsFr6piwYEDAkZD5JnCYkr31YUfDpdBCaN/sql.png)***Cheat sheet from a Udemy course***</center>

I code as a hobby. I like how simple codes can automate daily tasks, experiment with new ideas, and just be fun. Whenever I get a chance I do like to learn new coding skills. But one aspect of coding I always tried to avoid was dealing with databases. I did have experience in the past retrieving data from database using SQL. I learned about SQL from friends here, @carlgnash and @markangeltrueman. First time I used SQL was for curating purposes using SteemSQL(now HiveSQL). Usually there are some GUI programs to use SQL. I used it within a python code, so that I could do other operations on the data. In the following post I wrote about how to do the initial setup on Mac to use SQL commands in a python code:

https://hive.blog/utopian-io/@geekgirl/making-sql-queries-to-steemsql-com-with-python-scripts-on-macos

It can still be useful for those need to connect to a MSSQL server on a Mac computer. One change I would make is to use pyodcb, instead of pypyodcb as was suggested by @crokkon back then, and worked perfectly.

Last few days I have been trying to learn more about SQL, specifically PostgreSQL. It is open source, widely used, and powerful relational database. I almost finished [a Udemy course](https://www.udemy.com/course/the-complete-sql-bootcamp/) by Jose Portilla. I would highly recommend this Udemy course to anybody who is interested in learning SQL. Jose is a really good at teaching how to code. He also has useful courses on python, data science, etc. 

We can download PostgreSQL from PostgreSQL.org. To connect to the database, create databases, query data, etc we can use pgAdmin 4, which can be found at pgadmin.org. pgAdmin 4 runs on a browser and is really cool. I enjoyed using when learning how to SQL. However, my personal preference is to use SQL commands within python code. To do that we can use `psycopg2` module. The following code does just that, it allows to connect to a PostgreSQL within a python code. Afterwards we can execute any python operations we want with the retrieved data. Moreover, we should be able to create our own databases, tables, and store data all within python code.

```
import psycopg2 as pg2
import pprint

def queryDB(sql):
    database = 'awesome_database'
    user = 'postgres'
    password = 'XXXXXXXXXXXX'
    conn = pg2.connect(database=database, user=user, password=password)
    cur = conn.cursor()
    cur.execute(sql)
    result = cur.fetchmany(100)
    conn.close()
    return result

sql = '''SELECT * FROM awesome_table'''

result = queryDB(sql)
pprint.pprint(result)

```

By changing the content of the text inside `sql` variable we can execute any SQL commands. This code connects to a local database. To connect to a remote database we will need to provide details for `host` address and `port`.  For more details on `pscycopg2` visit Psycopg.org 

To experiment more with SQL, I want to explore the Hive blocks, learn what and how data is stored within Hive blocks, and store them in my own local PostgresSQL database. Of course not all of Hive, just few recent transactions. All of Hive blocks would probably take a lot of time and space. Doing so will give me an opportunity to understand Hive more, and experiment with SQL. I hope to share more about that next time.

Coding is fun!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 170 others
properties (23)
authorgeekgirl
permlinkconnecting-to-postgresql-database-and-making-queries-within-python-code
categorypython
json_metadata{"tags":["python","sql","coding","dev","neoxian","leo","ctptalk","palnet"],"users":["carlgnash","markangeltrueman","crokkon"],"image":["https://images.hive.blog/DQmXYEFZ8EC3AfsFr6piwYEDAkZD5JnCYkr31YUfDpdBCaN/sql.png"],"links":["https://hive.blog/utopian-io/@geekgirl/making-sql-queries-to-steemsql-com-with-python-scripts-on-macos","https://www.udemy.com/course/the-complete-sql-bootcamp/"],"app":"hiveblog/0.1","format":"markdown"}
created2020-09-10 01:40:33
last_update2020-09-10 01:40:33
depth0
children8
last_payout2020-09-17 01:40:33
cashout_time1969-12-31 23:59:59
total_payout_value8.072 HBD
curator_payout_value6.399 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,424
author_reputation1,215,196,225,598,670
root_title"Connecting to PostgreSQL database and making queries within Python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,545,904
net_rshares50,686,663,952,603
author_curate_reward""
vote details (234)
@chitty ·
I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!
properties (22)
authorchitty
permlinkre-connecting-to-postgresql-database-and-making-queries-within-python-code-20200912t000450
categorypython
json_metadata""
created2020-09-12 00:04:54
last_update2020-09-12 00:04:54
depth1
children0
last_payout2020-09-19 00:04:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length86
author_reputation86,901,300,608,582
root_title"Connecting to PostgreSQL database and making queries within Python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,580,265
net_rshares0
@edicted ·
$0.29
I'm using MySQL but PostgreSQL was a very strong second choice. 
The poker database I used back in the day was based on PostgreSQL.

Currently working on my Cards Against Humanity clone.  
Maybe I'll have you take a look at it when I have something relevant. 
👍  
properties (23)
authoredicted
permlinkre-geekgirl-qgf6zx
categorypython
json_metadata{"tags":["python"],"app":"peakd/2020.09.1"}
created2020-09-10 02:02:21
last_update2020-09-10 02:02:21
depth1
children4
last_payout2020-09-17 02:02:21
cashout_time1969-12-31 23:59:59
total_payout_value0.145 HBD
curator_payout_value0.145 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length259
author_reputation2,890,242,526,660,396
root_title"Connecting to PostgreSQL database and making queries within Python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,546,167
net_rshares1,475,682,095,848
author_curate_reward""
vote details (1)
@geekgirl ·
$0.03
What do you do with CAH clone?
👍  
properties (23)
authorgeekgirl
permlinkqgf7ih
categorypython
json_metadata{"app":"hiveblog/0.1"}
created2020-09-10 02:13:30
last_update2020-09-10 02:13:30
depth2
children3
last_payout2020-09-17 02:13:30
cashout_time1969-12-31 23:59:59
total_payout_value0.013 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation1,215,196,225,598,670
root_title"Connecting to PostgreSQL database and making queries within Python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,546,308
net_rshares162,044,645,024
author_curate_reward""
vote details (1)
@edicted ·
$0.13
Oh it's going to be an on-chain dapp.  Players will provably own their unique cards by sending 1 HBD to @null.  This is also how one farms governance tokens, that are used for a variety of game-related voting activities (like adding cards to the whitelist for example). 

It's kinda complicated but simple at the same time.  
I think I pretty much have the tokenomic logic all figured out at this point. 
Although no one really seems excited about it when I explain it so... who knows. 
👍  
properties (23)
authoredicted
permlinkre-geekgirl-qgf7ql
categorypython
json_metadata{"tags":["python"],"app":"peakd/2020.09.1"}
created2020-09-10 02:18:24
last_update2020-09-10 02:18:24
depth3
children2
last_payout2020-09-17 02:18:24
cashout_time1969-12-31 23:59:59
total_payout_value0.066 HBD
curator_payout_value0.067 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length486
author_reputation2,890,242,526,660,396
root_title"Connecting to PostgreSQL database and making queries within Python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,546,381
net_rshares725,278,418,033
author_curate_reward""
vote details (1)
@ibrahim51 ·
$0.10
I am learning about web security and ethical hacking. Though I am now in primary level , I am enjoying to much it. I use kali linux to learn it.Today I have learned about ddos and protection ddos. Its amazing.
👍  
properties (23)
authoribrahim51
permlinkqgge28
categorypython
json_metadata{"app":"hiveblog/0.1"}
created2020-09-10 17:32:36
last_update2020-09-10 17:32:36
depth1
children0
last_payout2020-09-17 17:32:36
cashout_time1969-12-31 23:59:59
total_payout_value0.048 HBD
curator_payout_value0.051 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length209
author_reputation790,476,549,652
root_title"Connecting to PostgreSQL database and making queries within Python code."
beneficiaries
0.
accounthiveonboard
weight100
1.
accountpeakd
weight300
2.
accountthreespeak
weight100
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,557,099
net_rshares586,533,661,733
author_curate_reward""
vote details (1)
@poshbot ·
https://twitter.com/geekjen/status/1303871437778821120
properties (22)
authorposhbot
permlinkre-connecting-to-postgresql-database-and-making-queries-within-python-code-20200910t014235z
categorypython
json_metadata"{"app": "beem/0.24.8"}"
created2020-09-10 01:42:36
last_update2020-09-10 01:42:36
depth1
children0
last_payout2020-09-17 01:42:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length54
author_reputation5,554,335,374,496
root_title"Connecting to PostgreSQL database and making queries within Python code."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,545,923
net_rshares0