PostgreSQL (2) Database Connection With Python Using "Psycopg2" Module

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ม.ค. 2025
  • Hello and welcome to the second video of PostgreSQL tutorial series.
    Today you are going to learn how to connect PostgreSQL database to Python program.
    As a matter of fact, there are several python modules that allow us to connect to and manipulate the database using PostgreSQL such as Psycopg2 which will be the module that we will be working with in this tutorial.
    Download link: github.com/Bek...
    Psycopg2 link: pypi.org/proje...
    DEV profile : dev.to/bekbrace
    Github profile: github.com/Bek...

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

  • @АркадийНалимов-р1э
    @АркадийНалимов-р1э 6 หลายเดือนก่อน +2

    Спасибо что так подробно. Такие как я имеют шанс чему-то научиться, благодаря таким как вы.

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

      @@АркадийНалимов-р1э Огромное Спасибо 🙏

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

    Thank you very much for this tutorial. Your teaching skill is really awesome.

    • @BekBrace
      @BekBrace  3 ปีที่แล้ว

      Thank you Alamin, this means a lot to me, friend 😊

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

      Hi @@BekBrace
      Your Video is really helpful, i am able to connect to DB but while in main i am getting like this in Run
      Connecting to postgreSQL Database ...
      connect() argument after ** must be a mapping, not NoneType
      Process finished with exit code 0
      its the same code as yours, please help!

  • @AshKatzEm
    @AshKatzEm 4 หลายเดือนก่อน +1

    Wow, awesome tutorial video. I've watched hundreds of technical tutorials and I've never commented on one but this one was "the best." You nicely showed us how to connect a python script to Postgres using psycopg2. That's where everyone else would end their video. But then you threw it out and showed us that with a little more work, we could do it way better. Beautiful programming too. Even though you probably didn't make up the syntax, your explanation of why each part was necessary and shouldn't be different was superb. Liked and Subscribed

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

      Thank you very very much for such kind words ! I appreciate you being a member of the family here, feel free always to ask your questions, and I will do my best to answer whenever possible. Thanks again my friend🙂

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

    Thanx for a nice lesson and clear pronunciation which is easy-to-understand !

    • @BekBrace
      @BekBrace  3 ปีที่แล้ว

      It’s a pleasure, Alex 🙏
      Всегда с удовольствием 🙂

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

    The ini approach is a pro move , thanks for the good job 👍

    • @BekBrace
      @BekBrace  4 ปีที่แล้ว

      Thank you for watching 😊

  • @JOHNDOE-ky9wz
    @JOHNDOE-ky9wz 4 ปีที่แล้ว +1

    Look forward for other sql parts , clear explaination , thxs alot

    • @BekBrace
      @BekBrace  4 ปีที่แล้ว

      Thanks , sure there will be more on PostgreSQL , MySQL and SQL Alchemy

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

    I'm a newbie. You left me in the dust on the config file...lolol But everything you said was clear to me. I just need to practice

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

      Great stuff, my friend

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

      hey im learning this now in 2024, how much did you progress in 3 years

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

      @@rohitmalviya8607 what about you? did you get the code?

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

      @@deshnajain1757 Ive been slacking since the last month never opened postgre after watching this video sorry

  • @TSKTECHIN
    @TSKTECHIN 7 หลายเดือนก่อน +1

    thanks for simple TuT, Great Going.

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

      You're very welcome

  • @mr.vooper8560
    @mr.vooper8560 7 หลายเดือนก่อน +1

    thank u for the video and for your pronounciation, so easy to make out, thx

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

      You're very welcome 🙏🙂

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

    Nice work , thank you for sharing the knowledge

    • @BekBrace
      @BekBrace  4 ปีที่แล้ว

      Glad it was useful 😊

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

    I am at 10:22 , at line 20 in config.py , have you putted "config()" only because you have mentionned the filename and the section when you defined the function above ?
    right ?

    • @BekBrace
      @BekBrace  3 ปีที่แล้ว

      At line 20, you’re invoking the config() function; and by running the config.py file, it’s executed to call that config() function- but you can change both names, it will still work

    • @ihebbibani7122
      @ihebbibani7122 3 ปีที่แล้ว

      @@BekBrace it wasn't my question. Actually , why is it possible to write the function with arguments and then call it without those arguments ; So what's the difference between a function with and without arguments in this case ?

    • @awdwadawda352
      @awdwadawda352 7 วันที่ผ่านมา

      ​@@ihebbibani7122 This has been 3 years, but for anyone reading this:
      It is because he is setting default values for the parameters/arguments. So it is always 'database.ini' and 'postgresql' unless specified otherwise.

    • @BekBrace
      @BekBrace  6 วันที่ผ่านมา

      Bingo was his name

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

    Wow very nice helpful 😀😇😜🤪😁

    • @BekBrace
      @BekBrace  2 หลายเดือนก่อน +1

      Glad you liked it

  • @user-ro9qc2ye7o
    @user-ro9qc2ye7o 3 ปีที่แล้ว +1

    Quality video on PostgreSQL, tysm man

    • @BekBrace
      @BekBrace  3 ปีที่แล้ว

      Thank you, Johanoid ,glad you found it useful.

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

    incredibly helpful video. one question, to get the dictionary from the list of tuples around 9:10, why not simply do db = dict(parser.items(section)) ?

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

      You can do that too 🙂

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

    Thank you.. its worth watching your tutorials..

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

      Much appreciated 👍

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

    Thank you🥰🥰

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

      You're welcome, Ahmed 🙂

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

    I am new to postgresql, what is the point of connecting postgresql to python? thanks

    • @BekBrace
      @BekBrace  ปีที่แล้ว +4

      Data Analysis: If you have data stored in a PostgreSQL database, you can use Python to connect to that database and extract, manipulate, and analyze the data. Python has powerful data analysis libraries like pandas, NumPy, and SciPy that can be used to perform complex data analysis tasks on the data stored in the PostgreSQL database.
      Web Development: PostgreSQL is a popular choice for storing data in web applications, and Python is a popular language for web development. Connecting PostgreSQL to Python allows you to use Python's web development frameworks like Django and Flask to build web applications that interact with the database.
      Automation: You can use Python to automate tasks that involve working with the PostgreSQL database. For example, you can write Python scripts that perform routine database maintenance tasks like backing up the database, checking for errors, and optimizing performance.
      Machine Learning: Python is a popular language for machine learning and artificial intelligence, and PostgreSQL can be used to store large amounts of data that are used in these fields. By connecting PostgreSQL to Python, you can build machine learning models that use data stored in the database.

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

      @@BekBrace Thanks a bunch sir

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

    Thanks very much.

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

      You're very welcome

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

    Fantastic video! I was wondering how you enable the description of classes or hints or arguments that go in on VS code. Is this an extension or does it come natively with intellisense? If so please suggest how to enable it? Liked and subscribed!

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

      Hi there! It's an installed plugin in VSCode called IntelliCode by Microsoft - download it for free.
      Thank you for watching and being active on the channel - Also you can consider being a member of the channel to get free project assessments, debugging tips and code reviews.

  • @deshnajain1757
    @deshnajain1757 4 หลายเดือนก่อน +1

    what do i need to learn, if i am not getting why this code is put, and how it is done? prerquisite? i know basic python

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

      @@deshnajain1757 oh. You should be familiar with database basics. I have a MySQL course you can check it out, then return back to this one

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

    Thank you, Bek

    • @BekBrace
      @BekBrace  4 ปีที่แล้ว

      Always a pleasure

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

    Great video. Your explanations are great, thank you. Why does 'port' not need to be included in the database.ini file?

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

      Thank you :) cause it's already defined ,🙂

  • @yltfy
    @yltfy 3 ปีที่แล้ว

    Nice video. Your voice is a bit mesmeric, though :)

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

      Thank you 😊 that’s not the first I hear that and I don’t know if it’s good or bad 😂

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

    I am getting "Section postgresql is not found in the database.ini file" error even if I write everything right. I have checked thousand times. What could be the reason of this?

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

      Have you “pip install configparser “ ? It’s a parser problem, Python cannot read what’s in your database.ini file

    • @jijobuje
      @jijobuje 3 ปีที่แล้ว

      @@BekBrace I will try.
      Btw thank you for the really quick reply. I would pay for the video and the service.

    • @jijobuje
      @jijobuje 3 ปีที่แล้ว

      @@BekBrace I am getting same error after installing configparser as well

    • @adrianlobacz4749
      @adrianlobacz4749 3 ปีที่แล้ว

      @@jijobuje i have the same problem

    • @adrianlobacz4749
      @adrianlobacz4749 3 ปีที่แล้ว

      I'm using code from your github and it's working. I dont know how it's possible

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

    i have a question how to open that 16:34

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

      Open what ?

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

      @@BekBrace the mingw64 cmd? Sorry im new to this

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

      @@kotch1 oh no problem. That is git bash, go to Google and type download git bash, it's free and easy to use

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

      @@BekBrace thank you sir!

  • @lionkingsroar664
    @lionkingsroar664 3 ปีที่แล้ว

    Thanku good video. Have a doubt when i opened the site psycopg2 there could see the option pip install psycopg2 and when click on it only i get a copy option. And where i paste that link

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

      To your command prompt or terminal to install psycopg2

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

    would you consider moving the connection block of codes, above the MAIN, to another file and just invoke it in the Main.py ?
    Nice video..

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

      Yes, absolutely

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

    Hey I am watching this video 3 years later and I like the concept but I am currently experiencing lots of errors when I run the main.py, connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "postgresql" this is the error I keep experiencing.
    Thanks in advance.

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

    I tried doing what you suggested but I got an error after executing the config.py file. It says:
    Traceback (most recent call last):
    File "C:\Users\Janmielou\OneDrive\Documents\PostgresSQL\config.py", line 20, in
    config()
    File "C:\Users\Janmielou\OneDrive\Documents\PostgresSQL\config.py", line 15, in config
    raise Exception('Section{0} is not found in the {1} file.'.format(section, filename))
    Exception: Sectionpostgresql is not found in the database.ini file.

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

      Add the [postgresql] section to your database.ini file with the required settings.

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

    the installating is not working on my laptop

    • @BekBrace
      @BekBrace  3 ปีที่แล้ว

      What’s the error ?

    • @jenex5608
      @jenex5608 3 ปีที่แล้ว

      @@BekBrace I'm installing it on a Linux machine. The build wheels did not install properly. I installed the required dependencies but still got an error. So I installed it from the Github source to gett it working

    • @jenex5608
      @jenex5608 3 ปีที่แล้ว

      @@BekBrace Also got this error
      Traceback (most recent call last):
      File "config.py", line 20, in
      config()
      File "config.py", line 11, in config
      params = parser.items(section)
      File "/usr/lib/python3.8/configparser.py", line 859, in items
      return [(option, value_getter(option)) for option in orig_keys]
      File "/usr/lib/python3.8/configparser.py", line 859, in
      return [(option, value_getter(option)) for option in orig_keys]
      File "/usr/lib/python3.8/configparser.py", line 855, in
      value_getter = lambda option: self._interpolation.before_get(self,
      File "/usr/lib/python3.8/configparser.py", line 395, in before_get
      self._interpolate_some(parser, option, L, value, section, defaults, 1)
      File "/usr/lib/python3.8/configparser.py", line 442, in _interpolate_some
      raise InterpolationSyntaxError(
      configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%'
      from the config.py

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

    thanks for the good job. I encountered such a problem. When I try to write the password wrong, print(error) is empty in the console. Why? postgres 10.15 psycopg2 2.8.6

    • @BekBrace
      @BekBrace  3 ปีที่แล้ว

      Can you paste the debug report, please?

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

      @@BekBrace I also only get an error if I use the wrong host, but not for wrong password or wrong database...

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

    Thank you for making such a great video.
    I encountered a problem with the main.py file. After running it I get the following error message:
    Conecting to the postgreSQL database ...
    invalid connection option "database"

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

      Change database to dbname in your connection parameters.

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

    really helpful, but i hope you can share the code since i found some mistake in my code (and i just know what the wrong until i found indent)

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

      Thank You

  • @chiedoziie
    @chiedoziie 11 วันที่ผ่านมา

    Nice video! can you help me psycopg2 keeps giving me an error.
    from psycopg2._psycopg import ( # noqa
    ......
    )
    ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

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

    it's a good video

  • @redrum4486
    @redrum4486 3 ปีที่แล้ว

    when i run the main.py file i get the following: {'host': 'localhost', 'database': 'master', 'user': 'postgres', 'password': 'admin'}
    {'host': 'localhost', 'database': 'master', 'user': 'postgres', 'password': 'admin'}
    Connecting to the postgreSQL database ...
    psycopg2.connect() argument after ** must be a mapping, not NoneType

    • @Zaid-lk2sy
      @Zaid-lk2sy 3 ปีที่แล้ว

      Make the function config() return db

  • @okbrb
    @okbrb 4 ปีที่แล้ว

    Why not just use a .env file and then read the value with load_dotenv() and os.getenv("db_port")

    • @BekBrace
      @BekBrace  4 ปีที่แล้ว

      You can.
      There are a lot of ways to connect the server to python, however using ini file is just my preferred approach to do it especially if you want to deploy your app on Heroku for instance

    • @ihebbibani7122
      @ihebbibani7122 3 ปีที่แล้ว

      @@BekBrace you're right , I have seen many githubs using your approach...

  • @luizferez89
    @luizferez89 3 ปีที่แล้ว

    6 idiots 😝 dislike this great video !!
    Thanks man

    • @BekBrace
      @BekBrace  3 ปีที่แล้ว

      Gracias 🙏 well, that’s ok, everyone to their opinion but thanks my friend

  • @ramnikbhorania4333
    @ramnikbhorania4333 หลายเดือนก่อน +1

    Thank you very much for this tutorial. Your teaching skill is really awesome.

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

      🙏🙏🙏