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

Поделиться
HTML-код
  • Опубликовано: 24 окт 2020
  • 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/BekBrace/postgresq...
    Psycopg2 link: pypi.org/project/psycopg2/
    DEV profile : dev.to/bekbrace
    Github profile: github.com/BekBrace

Комментарии • 76

  • @mr.vooper8560
    @mr.vooper8560 23 дня назад +1

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

    • @BekBrace
      @BekBrace  22 дня назад

      You're very welcome 🙏🙂

  • @JOHNDOE-ky9wz
    @JOHNDOE-ky9wz 3 года назад +1

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

    • @BekBrace
      @BekBrace  3 года назад

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

  • @leonkennedy6726
    @leonkennedy6726 3 года назад +3

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

    • @BekBrace
      @BekBrace  3 года назад

      Thank you for watching 😊

  • @filipfilip4184
    @filipfilip4184 2 года назад +3

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

    • @BekBrace
      @BekBrace  2 года назад

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

  • @alamin00038
    @alamin00038 2 года назад +6

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

    • @BekBrace
      @BekBrace  2 года назад

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

    • @anujaawasarmol9687
      @anujaawasarmol9687 Год назад

      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!

  • @user-ro9qc2ye7o
    @user-ro9qc2ye7o 3 года назад +1

    Quality video on PostgreSQL, tysm man

    • @BekBrace
      @BekBrace  3 года назад

      Thank you, Johanoid ,glad you found it useful.

  • @marcus7387
    @marcus7387 3 года назад +1

    Nice work , thank you for sharing the knowledge

    • @BekBrace
      @BekBrace  3 года назад

      Glad it was useful 😊

  • @leomacdon7858
    @leomacdon7858 Год назад

    Thank you.. its worth watching your tutorials..

    • @BekBrace
      @BekBrace  Год назад

      Much appreciated 👍

  • @TSKTECHIN
    @TSKTECHIN 27 дней назад +1

    thanks for simple TuT, Great Going.

    • @BekBrace
      @BekBrace  27 дней назад

      You're very welcome

  • @BRISKLYHIDDEN
    @BRISKLYHIDDEN 3 года назад +1

    Thank you, Bek

    • @BekBrace
      @BekBrace  3 года назад

      Always a pleasure

  • @helovesdata8483
    @helovesdata8483 3 года назад +2

    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

  • @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.

  • @brehymatraore
    @brehymatraore Год назад +1

    Thanks very much.

  • @georgetait864
    @georgetait864 Год назад +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  Год назад +1

      You can do that too 🙂

  • @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 ,🙂

  • @ahmed_abdoamin_abdo2844
    @ahmed_abdoamin_abdo2844 Год назад +1

    Thank you🥰🥰

    • @BekBrace
      @BekBrace  Год назад +1

      You're welcome, Ahmed 🙂

  • @ihebbibani7122
    @ihebbibani7122 2 года назад +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  2 года назад

      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 2 года назад

      @@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 ?

  • @peterluo1776
    @peterluo1776 7 месяцев назад

    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  7 месяцев назад

      Yes, absolutely

  • @yltfy
    @yltfy 2 года назад

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

    • @BekBrace
      @BekBrace  2 года назад +1

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

  • @samborms
    @samborms Год назад

    it's a good video

  • @lionkingsroar664
    @lionkingsroar664 2 года назад

    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  2 года назад +1

      To your command prompt or terminal to install psycopg2

  • @ryanmalik3378
    @ryanmalik3378 Год назад +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)

  • @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...

  • @redrum4486
    @redrum4486 2 года назад

    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 2 года назад

      Make the function config() return db

  • @yadali4833
    @yadali4833 Год назад +1

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

    • @BekBrace
      @BekBrace  Год назад +3

      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

  • @okbrb
    @okbrb 3 года назад

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

    • @BekBrace
      @BekBrace  3 года назад

      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 2 года назад

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

  • @luizferez89
    @luizferez89 2 года назад

    6 idiots 😝 dislike this great video !!
    Thanks man

    • @BekBrace
      @BekBrace  2 года назад

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

  • @jijobuje
    @jijobuje 2 года назад +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  2 года назад +2

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

    • @jijobuje
      @jijobuje 2 года назад

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

    • @jijobuje
      @jijobuje 2 года назад

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

    • @adrianlobacz4749
      @adrianlobacz4749 2 года назад

      @@jijobuje i have the same problem

    • @adrianlobacz4749
      @adrianlobacz4749 2 года назад

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

  • @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

  • @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!