Connect to PostgreSQL from Python (Using SQL in Python) | Python to PostgreSQL

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ค. 2024
  • Connect to PostgreSQL from Python (Using SQL in Python) | Python to PostgreSQL
    In this video, we shall understand how to connect to PostgreSQL Database from Python. We shall write a python program which will use the Psycopg2 module to connect to the PostgreSQL database and then perform some database or SQL transactions such as creating a table and then insert data to the table, updating a table data and delete records from the table using python code. We shall also see how to fetch table data from postgres database to python program.
    At the end of the video, I will also cover how to use Context Manager to perform all of the above SQL operations. That is, we will use the WITH clause to connect to the database and also use the WITH clause to open the cursor. I will also mention what are the advantages of using WITH clause and why it is recommended to always use context manager or WITH clause when working with database in python.
    Download the Python script used in this video from my blog. Link below:
    techtfq.com/video/connect-to-...
    Timeline:
    00:00 Intro
    00:42 Prerequisites for connecting to PostgreSQL from Python
    02:18 Connecting to PostgreSQL from Python
    05:33 Opening a cursor to perform database operations
    07:52 CREATE Table in PostgreSQL database from Python program
    10:37 INSERT single record to a table in PostgreSQL from Python program
    12:23 INSERT multiple records to PostgreSQL table from Python program
    14:30 FETCH data from PostgreSQL Table and display in Python program
    17:23 UPDATE Table data in PostgreSQL database from Python program
    18:50 DELETE Table record in PostgreSQL database from Python program
    20:14 Using Context Manager to connect to PostgreSQL database from Python
    We shall be using psycopg2 module to connect to PostgreSQL database from python, which is considered to be one of the best libraries to connect to postgres from python.
    We shall start the video by covering the couple of pre-requisites which is to first install the psycopg2 module and then finding the credentials or information required to connect to the postgres database.
    After this we shall write the python program which will initially connect to the PostgreSQL database and then open a cursor which will be then used to perform the database operations like CREATE TABLE, INSERT INTO Table, UPDATE Table data, DELETE table record and also how to fetch or retrieve table records or table data from the database to Python program.
    Finally, we shall end the video by talking about context manager way of connecting to the database and also how to use context manager to open a cursor. Using context manager is simply using the WITH clause while connecting to the database and then also using the WITH clause while opening the cursor.
    I will also mention the advantages of using a context manager over the traditional method or connecting to data and opening the cursor. And also mention why is it recommended to always use context manager when connecting to database in python.
    Hopefully this video helped and gave you some useful information of using SQL within python program. If you liked it then please make sure to like the video and also subscribe to the channel.
    🔴 WATCH MORE VIDEOS HERE 👇
    ✅ SQL Tutorial - Basic concepts:
    • SQL Tutorial - Basic c...
    ✅ SQL Tutorial - Intermediate concepts:
    • SQL Tutorial - Interme...
    ✅ SQL Tutorial - Advance concepts:
    • SQL Tutorial - Advance...
    ✅ Practice Solving Basic SQL Queries:
    • Practice Solving BASIC...
    ✅ Practice Solving Intermediate SQL Queries:
    • Practice Solving INTER...
    ✅ Practice Solving Complex SQL Queries:
    • Practice Solving COMPL...
    ✅ Data Analytics Career guidance:
    • Data Analytics career ...
    ✅ SQL Course, SQL Training Platform Recommendations:
    • SQL Course / Training
    ✅ Python Tutorial:
    • Python Tutorial
    ✅ Git and GitHub Tutorial:
    • Git and GitHub
    ✅ Data Analytics Projects:
    • Data Analytics Projects
    THANK YOU,
    Thoufiq

ความคิดเห็น • 171

  • @girijashamak9142
    @girijashamak9142 2 ปีที่แล้ว +46

    As a old woman learning to upgrade, I found your videos handy and very clear to catch up.I am one of your subscriber.I watched your SQL contents and simply loved it. So are your python concepts. Thank you for all your hardwork and is not easy to deliver.God bless and keep teaching people like me and others🙏

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว +6

      Hi Girija,
      This is such a beautiful feedback… I cannot express my happiness hearing that my videos have helped you ..
      Each video takes a lot of effort to make and it’s all worth it when I get to hear such beautiful words from you guys..
      I am so great full that you took your time to leave me this feedback..
      THANK YOU 🙏🏼
      And I wish you all the best in your learning journey 🙂

    • @dudeparak5654
      @dudeparak5654 2 ปีที่แล้ว

      installed paycopg2 for python pycharm and imported to the project.But the functions like curson() execute() are not autopopulated when I calling with the connection object

  • @gabip265
    @gabip265 ปีที่แล้ว +3

    Such a comprehensive and quick tutorial to get the basics! I recommend this tutorial after watching neural nine crash course for python and postgres. This really gives you more efficient and clean solutions for writting code.

  • @mtmanalyst
    @mtmanalyst ปีที่แล้ว +3

    This is really great stuff!
    I'm a math, ml, guy and I feel like I'm learning great python techniques here
    THANK YOU
    Morgan

  • @prathagautam9872
    @prathagautam9872 6 หลายเดือนก่อน +2

    Instantly subscribed!
    You mentioned all the details like preventing sql injection, using the 'with' clause, and so on (unlike other youtube tutors who just skip these ), so it's really helpful and you're a fantastic teacher! 💯

  • @ruthaipoh8373
    @ruthaipoh8373 2 ปีที่แล้ว +3

    This video was quite helpful. Saved me a lot of time and very comprehensive too. Thank you🙏

  • @NiceChange
    @NiceChange ปีที่แล้ว

    This video is pure gold!!! Thank you for doing such a great job on this.

  • @dimplesantoshi8561
    @dimplesantoshi8561 2 ปีที่แล้ว +2

    Very Nicely explained...Clarity was maintained from the beginning till the end of the video. Thank you so much!

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Thank you Dimple :)

  • @shakearound5789
    @shakearound5789 8 หลายเดือนก่อน +2

    You taught this fantastically! With your teaching I am able to immediately go away and use this knowledge on my own project - it's rare that anyone teaches me something that can enable me to do this with such ease. Look forward to watching more of your videos!

    • @techTFQ
      @techTFQ  7 หลายเดือนก่อน

      Thank you . Glad it helped

  • @NiceChange
    @NiceChange ปีที่แล้ว

    Dude. Love this video. demystifying postgres with python. Great work!!! this video goes in the vault. Thank you.

  • @Makingdifference100
    @Makingdifference100 2 ปีที่แล้ว +6

    You saved me today. Thanks, awesome video. Can you do more videos like this one, please? I love your method of teaching you are a natural teacher. Thank you! Absolutely a fantastic video!!!!!

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Thank you so much Santos and sure will do

  • @piyushtalegaonkar2940
    @piyushtalegaonkar2940 ปีที่แล้ว

    woooo man THANK YOU SO MUCH you helped me a lot bro, you literally made this so easy to understand and spot on
    I just started learning python and my manager is like I need results , you made my life easy by posting this video !!!!
    🙏🙏🙏

  • @MrutyunjayNayak
    @MrutyunjayNayak ปีที่แล้ว

    Brilliant video bro…I am learning python now. Your videos are crisp and self explanatory. Subscribed❤

  • @dogedingo1535
    @dogedingo1535 ปีที่แล้ว

    Very clear and instructions where easy to follow! Thanks for this video.

  • @user-os4lj3pi4q
    @user-os4lj3pi4q 4 หลายเดือนก่อน

    Very nice video. Not only showing how to get things done but also details like the try block, the little scripts (to reuse code) and stuff.

  • @DanjumaMuhammad
    @DanjumaMuhammad ปีที่แล้ว

    Very quick, clear and easy to catch. Thanks:)

  • @user-bl4nq4qg9u
    @user-bl4nq4qg9u 4 หลายเดือนก่อน

    The explanation is really clear! It is easy to understand what you are doing. Great job!

  • @kunrath
    @kunrath 2 ปีที่แล้ว +1

    Thx for the video, very useful and easy to understand! great job!

  • @mtmanalyst
    @mtmanalyst ปีที่แล้ว +1

    Excellent !!!
    Thank you
    Morgan

  • @okondivine4918
    @okondivine4918 ปีที่แล้ว

    This a fantastic video....Pure gold!

  • @ShenderRamos
    @ShenderRamos ปีที่แล้ว

    Great video, well explained, organized and efficient

  • @shiblyaziz9553
    @shiblyaziz9553 2 ปีที่แล้ว +2

    Thank you! Absolutely a fantastic video!!!!!. I love your teaching method.

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Thank you 🙏🏼
      Glad this helped

  • @diegofernandosotoflores7175
    @diegofernandosotoflores7175 10 หลายเดือนก่อน

    Gran video, acarreando el proyecto. Un grande Thoufiq

  • @ashaoluayanfe7474
    @ashaoluayanfe7474 ปีที่แล้ว

    Thank you so much for the video, I was having problems with rollback, any single mistake that I made, I had to start all over, but your video showed me a better approach.

  • @user-wb1ov4vh9x
    @user-wb1ov4vh9x 8 วันที่ผ่านมา

    Very helpful video.... Thank you so much😊😊

  • @hoanglam2814
    @hoanglam2814 ปีที่แล้ว

    short and concise. thank you!

  • @mickyelmb
    @mickyelmb 2 ปีที่แล้ว +2

    Great tutorial, very helpful. Thank you for posting it.

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Glad this helped

  • @karunakaranr2473
    @karunakaranr2473 ปีที่แล้ว

    I have been watching your tutorials and really awesome.. Thank you for your time and effort..

  • @anurasenarathna1703
    @anurasenarathna1703 2 ปีที่แล้ว +1

    Very informative and very clear. Thank you for sharing your knowledge.

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Your welcome Anura,
      Glad this helped

  • @mohammedshahil4898
    @mohammedshahil4898 2 ปีที่แล้ว +2

    Really helpful 👌🏻 Keep up the good work🙌🏻👍🏻

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Thank you hero ❤️🙏🏼

  • @ratneshraj4653
    @ratneshraj4653 ปีที่แล้ว

    Please continue making more dedicated videos in Python just like SQL.
    THANKS IN ADVANCE

  • @sivajyothichandra
    @sivajyothichandra ปีที่แล้ว

    Really. You covered the basic table with DDL, DML along with try, except, format string, tuple very understandable way. I heard using of iterators instead of for loop.

  • @pranshulsingh2216
    @pranshulsingh2216 ปีที่แล้ว

    Thank you so much, really it’s very helpful to me to understand how it’s working together . Thanks a lot.😊😊

  • @byte0me
    @byte0me 2 หลายเดือนก่อน

    Super helpful thank you!

  • @MyrLin8
    @MyrLin8 3 หลายเดือนก่อน

    on first blush this seems quite good :) subscribed.

  • @Rainmakeroffire
    @Rainmakeroffire ปีที่แล้ว

    This was very helpful man, thanks a lot.

  • @YoYoNo.1official
    @YoYoNo.1official 5 หลายเดือนก่อน +1

    THanks Bro Luv From India❤️

  • @maxx27i
    @maxx27i 2 ปีที่แล้ว

    Very nice explanation! 👏

  • @kondanikhilkumar2198
    @kondanikhilkumar2198 4 หลายเดือนก่อน

    Attracted to subscribe,Thank you

  • @Latif127
    @Latif127 8 หลายเดือนก่อน

    Thoufiq, it is a very clear, easy and helpful tutorial, you covered a lot. Thank you.

    • @techTFQ
      @techTFQ  7 หลายเดือนก่อน

      Welcome:)

  • @pushkarratnaparkhi2205
    @pushkarratnaparkhi2205 ปีที่แล้ว

    very useful video. Thanks a lot.

  • @status_dev
    @status_dev ปีที่แล้ว +1

    Thx for the video, very useful and easy to understand! great job!😀😀😀😀😀😀😀😀SUPER VIDEO

    • @techTFQ
      @techTFQ  ปีที่แล้ว

      Glad you liked it!!

  • @anduamlaktadesse9284
    @anduamlaktadesse9284 2 ปีที่แล้ว +1

    Thank you sir for everything! It's really appreciated!

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Glad you like them

  • @fathimafarahna2633
    @fathimafarahna2633 2 ปีที่แล้ว +1

    Superb.. You have an amazing explaining skills👍👍👍

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Thank you ❤️

  • @nigarsultana1739
    @nigarsultana1739 ปีที่แล้ว

    Thank you so much for your efforts!!!

  • @alexandrelucas5621
    @alexandrelucas5621 2 ปีที่แล้ว +1

    Very informative! Thanks!

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Glad you liked it

  • @Hlangwaneamukelani
    @Hlangwaneamukelani ปีที่แล้ว

    Great stuff, Thank You.

  • @azeszterifi
    @azeszterifi ปีที่แล้ว

    just brilliant!

  • @sacrupe
    @sacrupe 6 หลายเดือนก่อน

    this video was really helpful, thank you

  • @anubratadas1
    @anubratadas1 9 หลายเดือนก่อน

    Thank you for this great video

  • @WICKED21SUNNY
    @WICKED21SUNNY 2 ปีที่แล้ว

    very help video for me as I am new to python programming

  • @ErikS-
    @ErikS- ปีที่แล้ว

    Excellent video.

  • @sowmiyanadarajan8041
    @sowmiyanadarajan8041 2 ปีที่แล้ว +1

    Thanks for the video TFQ..I want more python tutorial and sql as well

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Noted bro , I’ll get to python soon

  • @festisanat1982
    @festisanat1982 ปีที่แล้ว

    Bro thank you a lot for the info . I love your method simple and easy.
    Keep the good work pls you a live saver. In programing I mean hh

  • @rics6035
    @rics6035 2 ปีที่แล้ว

    great video! thanks a lot

  • @SanjiVSwaraJ
    @SanjiVSwaraJ ปีที่แล้ว

    Awesome..

  • @stephenmusangi8976
    @stephenmusangi8976 ปีที่แล้ว

    Awesome video

  • @bendibhafed1687
    @bendibhafed1687 ปีที่แล้ว +1

    Great job Mr. Thoufiq.

    • @techTFQ
      @techTFQ  ปีที่แล้ว

      Thank you Hafed 🙏🏼

  • @mkgeidam
    @mkgeidam ปีที่แล้ว

    Excellent Python Tutorials keep it up!!! @techTFQ

  • @Simple.Simple779
    @Simple.Simple779 ปีที่แล้ว

    you are my life saver...

  • @samucancld
    @samucancld 2 ปีที่แล้ว

    buenísimo loko

  • @mariacamiladurangobarrera2821
    @mariacamiladurangobarrera2821 ปีที่แล้ว

    Thank you!

  • @ROMAN-zu3wu
    @ROMAN-zu3wu 2 ปีที่แล้ว

    Great tutorial bro

  • @makhmud_jumanazarov
    @makhmud_jumanazarov ปีที่แล้ว

    thank you for useful content

  • @sssaamm29988
    @sssaamm29988 2 ปีที่แล้ว +2

    Wow,just one watch i could grasp most of the content.Havent felt boring at any point of time.great job!

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว +1

      Glad you liked it Sampath

  • @FIXENDIARY
    @FIXENDIARY หลายเดือนก่อน

    Superb

  • @nadeeradulan9231
    @nadeeradulan9231 ปีที่แล้ว

    Thanks Bro!

  • @hoanglam2814
    @hoanglam2814 ปีที่แล้ว

    love this one!

  • @gururajarao7833
    @gururajarao7833 11 หลายเดือนก่อน

    very good video. You could have also covered how to use config file for database connection parameters

  • @akashsivasubramanian4067
    @akashsivasubramanian4067 2 ปีที่แล้ว +1

    very clear. thanks a lot.

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      You are welcome Akash!

  • @yayasssamminna
    @yayasssamminna หลายเดือนก่อน

    suggestion : make a video explaining what goes into a config file and where to find all that information. I have trouble understanding how to switch from database administration to the rest of my code. thanks, great video !!!

  • @umaanil3344
    @umaanil3344 2 ปีที่แล้ว +1

    Great tutorial..👏👏

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Thank you Anil 🙏🏼

  • @radhakrishnanaik5656
    @radhakrishnanaik5656 2 ปีที่แล้ว +2

    You did great job and explained in simple steps.can you please prepare a video on exporting CSV from postgreySQL to python in pipeline?

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      thank you Naik and sure will consider it for the future

  • @malavans
    @malavans ปีที่แล้ว

    Excellent video!!! One thing to clarify, what’s the difference between connecting this through SQLAlchemy with psycopg2 driver and connecting via psycopg2 driver straight away.

  • @saic3149
    @saic3149 2 ปีที่แล้ว +2

    Super explanation really helpful… please cover ‘csv’ module in python

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Thank you so much 🙏🏼
      Really glad you found this helpful 🙂
      Sure will consider your feedback to make a video on csv module..

  • @younesdeveloper
    @younesdeveloper 4 หลายเดือนก่อน

    top one

  • @sivahanuman4466
    @sivahanuman4466 ปีที่แล้ว +1

    Great sir

    • @techTFQ
      @techTFQ  ปีที่แล้ว

      Thank you ☺️

  • @cruizersofthemilleni
    @cruizersofthemilleni ปีที่แล้ว

    Thank you so much. Your video was really helpful. Do you have similar videos on pandas data frame using PostgreSQL? Thanks again.

  • @jessemonisha8018
    @jessemonisha8018 ปีที่แล้ว

    Super

  • @gvs485
    @gvs485 6 หลายเดือนก่อน

    tysm!

  • @MunishReddy
    @MunishReddy ปีที่แล้ว

    This is amazing stuff. Thank you, brother. I am trying to send the outputs I got in python to the database. Like I am reading valves from my plc using the OPC UA server via python. Now I want to send those values to my PostgreSQL database. Any help would be appreciated. Thank you very much again.

  • @anilsana105
    @anilsana105 ปีที่แล้ว +1

    clear one

    • @techTFQ
      @techTFQ  ปีที่แล้ว

      Thank you :)

  • @nihalmohammed8093
    @nihalmohammed8093 ปีที่แล้ว +1

    111,000th Subscriber

  • @zauraxmed9622
    @zauraxmed9622 ปีที่แล้ว

    thank you

  • @kashmirshadows8150
    @kashmirshadows8150 2 ปีที่แล้ว +1

    Thank you Thoufiq

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Your welcome 🙏🏼

  • @ruphashree
    @ruphashree 7 หลายเดือนก่อน

    Thank you sir

    • @techTFQ
      @techTFQ  7 หลายเดือนก่อน

      Welcome

  • @GuillermoPalchik
    @GuillermoPalchik 10 หลายเดือนก่อน

    Great video. How does one go about creating, storing, and using the config file?

  • @TheToric
    @TheToric 2 ปีที่แล้ว

    given that the context manager does not close the connection, is there any advantage of putting the connection under a contex manager?

  • @itsupportbyharshit6285
    @itsupportbyharshit6285 ปีที่แล้ว

    thanks

  • @rubang973
    @rubang973 2 ปีที่แล้ว +1

    Thx buddy

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว

      Glad you liked it

  • @vinoddevasani
    @vinoddevasani ปีที่แล้ว

    This is awesome, But can we print the column names also in the Console/Output? Your code is just displaying the data.

  • @timtech9361
    @timtech9361 2 ปีที่แล้ว +1

    Question. When creating the table, for the PRIMARY KEY which is id. Can you use SERIAL instead of INT? Asking since
    SERIAL will automatically address this as the PRIMARY KEY and the id will always be unique? I am assuming since the id will not have duplicates.

    • @techTFQ
      @techTFQ  2 ปีที่แล้ว +1

      Yea you can use SERIAL as the data type for the primary key ID column..
      I just used INT for this particular example

  • @mohammedkarimuddin8472
    @mohammedkarimuddin8472 11 หลายเดือนก่อน

    Which database is mostly used in IT industry Oracle or PostgreSql.
    Can you please tell me

  • @pusapatinarasimharaju956
    @pusapatinarasimharaju956 2 ปีที่แล้ว

    sir what is the use of python to postgresql.we can dirctly write sql in postgresql in database for all the operations.please reply

  • @pyalgoGPT
    @pyalgoGPT ปีที่แล้ว

    As a token of gratitude, I can buy your Udemy course or give a 5satr rating... God bless you. Create a video on polar with postgres

  • @ravendrink2710
    @ravendrink2710 ปีที่แล้ว

    Could you please tell me how to store or print, how many rows are getting affected after performing a DML.

  • @ha-tibas4208
    @ha-tibas4208 ปีที่แล้ว

    es lindo

  • @GIS-Engineer
    @GIS-Engineer 2 ปีที่แล้ว

    When we run this command pg_lsclusters then output will give status is down..then we run systemctl restart postgresql.
    It will work fine but after one or two hour it will again down..how will I resolved..plz help me..

  • @tejaskifamily
    @tejaskifamily 2 ปีที่แล้ว

    please create a video how to import data from CSV or excel file (of MB or GB data size) in tables automatically using python.

  • @siddhusathi
    @siddhusathi 2 ปีที่แล้ว

    Hello, can you please explain how to fetch million rows from postgresql with backend API and pass to frontend framework. Thanks in advance

  • @shivampandey7373
    @shivampandey7373 2 ปีที่แล้ว

    For example if data is available in GCS bucket then how we will load data in postgre table..