WEBVTT

00:00:00.180 --> 00:00:01.580
<v Jose>Hi guys and welcome back.</v>

00:00:01.580 --> 00:00:03.490
In this video, we're going to learn about

00:00:03.490 --> 00:00:07.380
psycopg2 and psycopg2-binary.

00:00:07.380 --> 00:00:10.850
These are the two releases of the psycopg2 library

00:00:10.850 --> 00:00:13.330
that we're going to use to interact with Postgres

00:00:13.330 --> 00:00:15.330
from within Python.

00:00:15.330 --> 00:00:19.750
The what SQLite3 is for SQLite, psycopg2 is for Postgres.

00:00:19.750 --> 00:00:21.500
It's essentially a Python library

00:00:21.500 --> 00:00:23.430
that allows us to interact with the database

00:00:23.430 --> 00:00:24.510
in an easy way.

00:00:24.510 --> 00:00:27.140
SQLite3 is simpler than psycopg2,

00:00:27.140 --> 00:00:29.170
because SQLite is simpler than PostgreS.

00:00:29.170 --> 00:00:33.320
There's simply more things that we can do with psycopg2,

00:00:33.320 --> 00:00:35.500
but many things will remain similar.

00:00:35.500 --> 00:00:39.730
However, there's an initial hurdle when installing psygopg2.

00:00:39.730 --> 00:00:41.830
Unfortunately this is a very unfortunate hurdle.

00:00:41.830 --> 00:00:43.301
Something that puts off a lot of people

00:00:43.301 --> 00:00:46.200
and that's that there's two psycopg2's.

00:00:46.200 --> 00:00:49.540
There's psycopg2 and psycopg2-binary

00:00:49.540 --> 00:00:51.410
and lot of people get confused

00:00:51.410 --> 00:00:52.950
over which one to install.

00:00:52.950 --> 00:00:54.780
A lot of people have trouble installing them.

00:00:54.780 --> 00:00:56.430
So in this video, we just wanted to tell you

00:00:56.430 --> 00:00:58.250
a little bit about the differences

00:00:58.250 --> 00:01:00.560
and also which one you should install.

00:01:00.560 --> 00:01:02.650
So, for most people,

00:01:02.650 --> 00:01:05.070
especially if you're new to software development

00:01:05.070 --> 00:01:06.320
or you're only just starting,

00:01:06.320 --> 00:01:09.120
of if you're using a Windows computer,

00:01:09.120 --> 00:01:12.890
you should use psycopg2-binary.

00:01:12.910 --> 00:01:14.270
They are essentially identical,

00:01:14.270 --> 00:01:17.070
however, when you're using psycopg2-binary,

00:01:17.070 --> 00:01:19.050
you can run into some trouble later on

00:01:19.050 --> 00:01:21.080
when you're doing some really advanced stuff.

00:01:21.080 --> 00:01:22.940
Never install both.

00:01:22.940 --> 00:01:24.770
So only install one of them.

00:01:24.770 --> 00:01:27.390
If you install, say psycopg2

00:01:27.390 --> 00:01:28.790
and then you find an error

00:01:28.790 --> 00:01:31.370
and then you try to install psycopg2-binary,

00:01:31.370 --> 00:01:33.170
make sure to remove,

00:01:33.170 --> 00:01:36.390
completely uninstall psycopg2 first.

00:01:36.390 --> 00:01:39.560
But again, I recommend installing psycopg2-binary

00:01:39.560 --> 00:01:42.030
if you are slightly newer to software development

00:01:42.030 --> 00:01:43.600
or if you're only just starting.

00:01:43.600 --> 00:01:45.100
So what are the differences?

00:01:45.100 --> 00:01:47.910
Well, the main difference is that you need to build psycopg2

00:01:47.910 --> 00:01:50.030
and that means you need a lot of other programmes

00:01:50.030 --> 00:01:50.940
to make it work,

00:01:50.940 --> 00:01:52.640
because when you install psycopg2

00:01:52.640 --> 00:01:56.040
what you get is the source code of psycopg2.

00:01:56.040 --> 00:01:59.070
And that code, in order to actually make it work,

00:01:59.070 --> 00:02:01.550
needs a lot of stuff from PostgreS

00:02:01.550 --> 00:02:02.680
and from a few other programmes

00:02:02.680 --> 00:02:05.330
in order to actually, you know, build.

00:02:05.330 --> 00:02:08.490
And so that means it's a little bit more complicated to do.

00:02:08.490 --> 00:02:09.570
You can run into some errors,

00:02:09.570 --> 00:02:11.960
especially if you have any programmes missing

00:02:11.960 --> 00:02:14.500
and also sometimes psycopg2

00:02:14.500 --> 00:02:17.010
won't know where your programmes are

00:02:17.010 --> 00:02:18.850
and you have to tell it where they are.

00:02:18.850 --> 00:02:21.758
And all of those things add to some complication.

00:02:21.758 --> 00:02:24.540
Psycopg2-binary should just work.

00:02:24.540 --> 00:02:26.660
But again, like I mentioned when we do some really advanced

00:02:26.660 --> 00:02:29.140
and specialised things, it can have some trouble,

00:02:29.140 --> 00:02:31.540
but we're not gonna get to those things in this course.

00:02:31.540 --> 00:02:33.080
You don't have to worry about that.

00:02:33.080 --> 00:02:36.670
And also, we normally deploy databases to a server

00:02:36.670 --> 00:02:37.503
on the internet,

00:02:37.503 --> 00:02:39.980
we will learn more about what that means later on.

00:02:39.980 --> 00:02:41.180
And when you do that,

00:02:41.180 --> 00:02:43.560
you'll be able to build psycopg2 without trouble.

00:02:43.560 --> 00:02:45.540
I also left many references for you to read

00:02:45.540 --> 00:02:46.610
if you're interested.

00:02:46.610 --> 00:02:47.770
They can be a bit technical,

00:02:47.770 --> 00:02:49.570
but again, if you want to learn more about

00:02:49.570 --> 00:02:51.980
why I'm recommending psycopg2-binary

00:02:51.980 --> 00:02:53.830
and why there's two and so on,

00:02:53.830 --> 00:02:55.290
then you can read those.

00:02:55.290 --> 00:02:57.070
So to install psycopg2-binary,

00:02:57.070 --> 00:02:58.760
I'm gonna show you how just now,

00:02:58.760 --> 00:03:00.520
but essentially, you just find it in PyCharm

00:03:00.520 --> 00:03:02.050
or you type PIPinstall

00:03:02.050 --> 00:03:03.440
and then it'll work

00:03:03.440 --> 00:03:06.860
but you do need to upgrade PIP and Setup Tools first.

00:03:06.860 --> 00:03:11.820
And you also need Python2.7 or Python 3.4 or greater.

00:03:11.820 --> 00:03:12.960
As with the rest of this course,

00:03:12.960 --> 00:03:15.770
we're gonna use the latest version of Python.

00:03:15.770 --> 00:03:17.120
All right, let's get to it.

00:03:18.630 --> 00:03:19.820
All right, so like I mentioned,

00:03:19.820 --> 00:03:21.770
since we're now using PostgreSQL,

00:03:21.770 --> 00:03:23.290
and we're gonna start building larger

00:03:23.290 --> 00:03:24.910
and more complicated projects,

00:03:24.910 --> 00:03:26.650
we're gonna begin using PyCharm

00:03:26.650 --> 00:03:29.300
and virtual environments to manage our projects

00:03:29.300 --> 00:03:30.520
a bit better.

00:03:30.520 --> 00:03:33.020
If you don't know how to get PyCharm or what it is,

00:03:33.020 --> 00:03:35.120
or you don't know about virtual environments

00:03:35.120 --> 00:03:36.110
and what they are,

00:03:36.110 --> 00:03:37.180
I've left a couple of links

00:03:37.180 --> 00:03:39.230
in the resources section of this lecture,

00:03:39.230 --> 00:03:40.870
for you to read.

00:03:40.870 --> 00:03:44.020
We start off by creating a new project in PyCharm,

00:03:44.020 --> 00:03:45.470
and what we wanna do here

00:03:45.470 --> 00:03:47.470
is we wanna make sure to find a folder

00:03:47.470 --> 00:03:49.840
where we wanna put our entire project.

00:03:49.840 --> 00:03:51.980
And then, just at the end of it,

00:03:51.980 --> 00:03:53.410
add the name of our project,

00:03:53.410 --> 00:03:55.100
for example, movies.

00:03:55.100 --> 00:03:56.600
What PyCharm is gonna do,

00:03:56.600 --> 00:03:58.690
you need to make sure that the movies folder

00:03:58.690 --> 00:04:00.290
doesn't exist already.

00:04:00.290 --> 00:04:02.270
What PyCharm is gonna do is it's gonna create

00:04:02.270 --> 00:04:05.560
the movies folder inside this folder

00:04:05.560 --> 00:04:08.380
and it's gonna put the PyCharm project files in there.

00:04:08.380 --> 00:04:11.380
If the file already exists, then PyCharm is gonna complain.

00:04:11.380 --> 00:04:13.690
Now, for the project interpreter,

00:04:13.690 --> 00:04:17.097
we're gonna click on New Interpreter, using Virtualenv,

00:04:17.097 --> 00:04:19.160
then we're gonna leave the location as the default,

00:04:19.160 --> 00:04:21.420
pretty much and the base interpreter,

00:04:21.420 --> 00:04:23.430
we're gonna select Python3.8

00:04:23.430 --> 00:04:25.380
of if you're using a different Python version,

00:04:25.380 --> 00:04:27.780
then the Python version that you want to use.

00:04:27.780 --> 00:04:30.640
I'm gonna use 3.8 because we wanna use the latest version

00:04:30.640 --> 00:04:32.700
of Python in this course.

00:04:32.700 --> 00:04:34.670
Now, when we create a new virtual environment,

00:04:34.670 --> 00:04:36.250
what that's gonna do, essentially,

00:04:36.250 --> 00:04:38.590
is copy Python into this folder

00:04:38.590 --> 00:04:43.380
and give us a new version of Python using Python3.8

00:04:43.380 --> 00:04:45.340
that is a copy of the existing one.

00:04:45.340 --> 00:04:50.070
But it's gonna allow us to install libraries in here

00:04:50.070 --> 00:04:53.220
that don't affect or are not shared with

00:04:53.220 --> 00:04:56.410
projects that run on the base interpreter.

00:04:56.410 --> 00:04:57.290
What happens eventually,

00:04:57.290 --> 00:04:59.480
is that you can have a lot of different virtual environments

00:04:59.480 --> 00:05:02.400
each one with its own set of libraries installed

00:05:02.400 --> 00:05:04.330
that don't conflict with one another.

00:05:04.330 --> 00:05:06.630
For example, if two projects wanted to use,

00:05:06.630 --> 00:05:08.800
you know, different versions of the same library

00:05:08.800 --> 00:05:10.480
and things like that.

00:05:10.480 --> 00:05:11.900
All right, so let's click created

00:05:11.900 --> 00:05:14.940
and then we will land up with our project created,

00:05:14.940 --> 00:05:17.450
PyCharm also likes to give us tips of the day and so forth.

00:05:17.450 --> 00:05:20.060
You can read them to learn how to use PyCharm a bit better.

00:05:20.060 --> 00:05:21.940
It's all well and good.

00:05:21.940 --> 00:05:24.260
Now once we've got that,

00:05:24.260 --> 00:05:27.530
we want to go ahead and update PIP.

00:05:27.530 --> 00:05:31.390
PIP is used to install Python packages or libraries,

00:05:31.390 --> 00:05:32.780
so we want to update it.

00:05:32.780 --> 00:05:34.880
There are two ways of doing this

00:05:34.880 --> 00:05:37.270
and one way is probably gonna be faster

00:05:37.270 --> 00:05:38.660
and easier for you,

00:05:38.660 --> 00:05:41.350
and that is if you go over to the settings.

00:05:41.350 --> 00:05:43.010
Sorry, I went a bit quickly there.

00:05:43.010 --> 00:05:44.400
To the settings here in PyCharm,

00:05:44.400 --> 00:05:46.810
it's called Preferences on Mac.

00:05:46.810 --> 00:05:50.620
Then find the project, project interpreter,

00:05:50.620 --> 00:05:51.993
double click on PIP,

00:05:52.880 --> 00:05:54.720
specify the version

00:05:54.720 --> 00:05:56.910
and make sure to match the number here

00:05:56.910 --> 00:05:58.830
with the one up there.

00:05:58.830 --> 00:06:00.940
And then press install.

00:06:00.940 --> 00:06:03.730
However, I've got a bit of a weird installation here

00:06:03.730 --> 00:06:04.563
on my computer,

00:06:04.563 --> 00:06:06.410
so this means this is not working for me at the moment,

00:06:06.410 --> 00:06:08.860
I still need to debug it and figure out why not.

00:06:08.860 --> 00:06:10.760
But this should work for most of you.

00:06:10.760 --> 00:06:12.000
If this doesn't work though,

00:06:12.000 --> 00:06:13.060
it's good that I have a problem,

00:06:13.060 --> 00:06:15.140
because I can show you an alternative way of doing it

00:06:15.140 --> 00:06:16.400
that will work.

00:06:16.400 --> 00:06:17.360
If that doesn't work,

00:06:17.360 --> 00:06:19.250
click on terminal

00:06:19.250 --> 00:06:22.000
and then make sure that you've got this pair of brackets

00:06:22.000 --> 00:06:23.410
with movies at the start.

00:06:23.410 --> 00:06:24.243
That's gonna show you

00:06:24.243 --> 00:06:26.470
that you are currently in a virtual environment

00:06:26.470 --> 00:06:28.100
and anything you install

00:06:28.100 --> 00:06:31.320
is going to be installed in the virtual environment.

00:06:31.320 --> 00:06:35.710
And then we're gonna type easy_install -U,

00:06:35.710 --> 00:06:40.710
sorry that's _install, -U, pip.

00:06:40.940 --> 00:06:42.950
And that's going to update PIP.

00:06:42.950 --> 00:06:45.920
Now, again, try to use the other method if you can

00:06:45.920 --> 00:06:47.400
because it's gonna be easier.

00:06:47.400 --> 00:06:48.760
So once we've go this,

00:06:48.760 --> 00:06:53.350
we can go ahead and install psycopg2-binary.

00:06:53.350 --> 00:06:55.970
So again, we can go over to the settings

00:06:55.970 --> 00:06:57.200
or preferences on Mac,

00:06:57.200 --> 00:06:59.950
make sure that we've got this PIP installed there

00:06:59.950 --> 00:07:03.690
and we're going to simply click on the plus icon.

00:07:03.690 --> 00:07:07.800
Find here psycopg2-binary.

00:07:07.800 --> 00:07:09.800
Make sure to spell things correctly there

00:07:09.800 --> 00:07:12.643
and just install the package like that.

00:07:13.630 --> 00:07:15.650
Hopefully it'll install successfully.

00:07:15.650 --> 00:07:17.100
If it doesn't, just leave a question

00:07:17.100 --> 00:07:17.933
in the course Q and A

00:07:17.933 --> 00:07:19.110
and we'll help you out with that

00:07:19.110 --> 00:07:20.510
and press OK then.

00:07:20.510 --> 00:07:22.140
Now to verify that it works,

00:07:22.140 --> 00:07:23.820
we're gonna create a new file.

00:07:23.820 --> 00:07:25.590
So we're gonna right click on our project,

00:07:25.590 --> 00:07:27.000
that's the movies project.

00:07:27.000 --> 00:07:28.483
Click on New Python File.

00:07:29.360 --> 00:07:32.010
And then create it, for example, app.py

00:07:32.010 --> 00:07:33.250
and here all we have to do

00:07:33.250 --> 00:07:35.363
is type import psycopg2,

00:07:36.400 --> 00:07:38.190
right click on app.py,

00:07:38.190 --> 00:07:39.780
and run it.

00:07:39.780 --> 00:07:42.090
Hopefully you get nothing.

00:07:42.090 --> 00:07:45.130
No red angry text telling you there's an error.

00:07:45.130 --> 00:07:47.160
If you do get angry red text,

00:07:47.160 --> 00:07:49.530
then try to find out why that is,

00:07:49.530 --> 00:07:50.930
maybe put the error in Google

00:07:50.930 --> 00:07:52.100
and see if you can figure it out

00:07:52.100 --> 00:07:53.850
because there can be a few things that happen.

00:07:53.850 --> 00:07:55.640
But if you find that you can't figure it out,

00:07:55.640 --> 00:07:56.787
leave a question in the course Q and A

00:07:56.787 --> 00:07:58.950
and we can help you find a solution.

00:07:58.950 --> 00:08:00.130
'Course, we don't know the answers

00:08:00.130 --> 00:08:01.850
to every possible error that can happen

00:08:01.850 --> 00:08:04.290
when installing psycopg2-binary

00:08:04.290 --> 00:08:05.610
but we can help you out.

00:08:05.610 --> 00:08:08.360
But, hopefully, you get no issues whatsoever

00:08:08.360 --> 00:08:10.810
and everything works just fine,

00:08:10.810 --> 00:08:14.260
then we are ready to start using psycopg2

00:08:14.260 --> 00:08:17.430
to actually connect to a PostgreS database.

00:08:17.430 --> 00:08:20.690
So, this was a long installation process,

00:08:20.690 --> 00:08:21.920
but now that we're here,

00:08:21.920 --> 00:08:24.270
all we have to do is import psycopg2

00:08:25.110 --> 00:08:28.880
and then in the URL, enter your ElephantSQL URL,

00:08:28.880 --> 00:08:30.470
so that's the entire thing that we got

00:08:30.470 --> 00:08:33.220
from ElephantSQL, I'm just gonna bring it up right now,

00:08:37.500 --> 00:08:38.650
So just paste it in there,

00:08:38.650 --> 00:08:41.590
I know it's pretty long but don't worry about that.

00:08:41.590 --> 00:08:43.580
And then what we wanna do is connect.

00:08:43.580 --> 00:08:47.330
So we'll do something like connection = psycopg2.connect

00:08:47.330 --> 00:08:49.110
and paste in the URL.

00:08:49.110 --> 00:08:52.470
Psycopg2 is smart enough to extract a username,

00:08:52.470 --> 00:08:54.190
password and host name and port name

00:08:54.190 --> 00:08:56.050
and everything from this URL

00:08:56.050 --> 00:08:57.850
so you don't have to do it yourself.

00:08:57.850 --> 00:09:01.700
Now, in psycopg2, as we will learn in the next few videos,

00:09:01.700 --> 00:09:04.950
we always have to create a cursor to execute a query.

00:09:04.950 --> 00:09:07.910
It's not like SQLite where we can execute queries directly

00:09:07.910 --> 00:09:11.000
on the connection and it creates a cursor for us.

00:09:11.000 --> 00:09:12.370
We have to do it ourselves,

00:09:12.370 --> 00:09:16.490
so we'll do cursor = connection.cursor

00:09:16.490 --> 00:09:17.580
and then we can do something like

00:09:17.580 --> 00:09:21.763
cursor.execute select star from users.

00:09:23.060 --> 00:09:25.880
And then we can, for example, fetch the first row.

00:09:25.880 --> 00:09:29.810
So at first user is equal to cursor.fetchone

00:09:31.830 --> 00:09:33.893
and then we can print the first user.

00:09:34.930 --> 00:09:37.833
Remember, always at the end, connection.close.

00:09:38.800 --> 00:09:40.100
All right, let's run this.

00:09:40.100 --> 00:09:41.670
Something important to keep in mind

00:09:41.670 --> 00:09:42.990
when working with PyCharm

00:09:42.990 --> 00:09:46.930
is that there are two main ways of running anything.

00:09:46.930 --> 00:09:48.880
You could right click on the file and run it

00:09:48.880 --> 00:09:52.350
and this is the sure-fire code you want to run.

00:09:52.350 --> 00:09:53.270
And now the other way

00:09:53.270 --> 00:09:55.400
is to click the play button here at the top right,

00:09:55.400 --> 00:09:56.740
but then when you click that,

00:09:56.740 --> 00:09:58.980
what you're running is this.

00:09:58.980 --> 00:10:01.850
This here is a run configuration

00:10:01.850 --> 00:10:03.770
and at the moment we only have one,

00:10:03.770 --> 00:10:05.500
because we only have one file.

00:10:05.500 --> 00:10:07.830
Whenever you run a file in PyCharm,

00:10:07.830 --> 00:10:10.180
PyCharm will create a run configuration for you

00:10:10.180 --> 00:10:11.470
and save it up here.

00:10:11.470 --> 00:10:12.670
You can then select one

00:10:12.670 --> 00:10:15.580
and run it with the play button there.

00:10:15.580 --> 00:10:17.740
So here you can see that we ran all of this

00:10:17.740 --> 00:10:21.630
and we get the output which is our user from our table.

00:10:21.630 --> 00:10:24.190
If you're confused as to why we're importing psycopg2

00:10:24.190 --> 00:10:25.390
and not psycopg2-binary,

00:10:26.410 --> 00:10:28.950
it's because we're only ever going to install either

00:10:28.950 --> 00:10:31.860
psycopg2-binary or psycopg2.

00:10:31.860 --> 00:10:35.500
They both create the psycopg2 package

00:10:35.500 --> 00:10:36.780
in our virtual environment.

00:10:36.780 --> 00:10:38.360
They just go about creating it

00:10:38.360 --> 00:10:40.430
and installing it in two different ways.

00:10:40.430 --> 00:10:43.120
That's why one of them is easier to install than the other.

00:10:43.120 --> 00:10:46.270
But in both cases, you end up with the exact same code

00:10:46.270 --> 00:10:47.860
that we interact with.

00:10:47.860 --> 00:10:49.340
All right, now that we've created a project

00:10:49.340 --> 00:10:51.720
and tested that psycopg2 is working,

00:10:51.720 --> 00:10:53.480
we're ready to start migrating

00:10:53.480 --> 00:10:56.250
our movie watchlist project from the last section

00:10:56.250 --> 00:10:58.690
over to psycopg2.

00:10:58.690 --> 00:11:02.000
So to start, I would delete this file here.

00:11:02.000 --> 00:11:03.520
So I'm gonna go ahead and do that

00:11:03.520 --> 00:11:05.020
because we don't need it anymore,

00:11:05.020 --> 00:11:07.940
and copy all of the movie watchlist code files

00:11:07.940 --> 00:11:11.370
into this project so that we can start the migration.

00:11:11.370 --> 00:11:12.750
All right, let's do that

00:11:12.750 --> 00:11:13.890
and I'll meet you again here

00:11:13.890 --> 00:11:15.170
in the next video.

00:11:15.170 --> 00:11:16.003
See you soon.