1
00:00:00,625 --> 00:00:02,032
- [Instructor] Hi and
welcome back to the course.

2
00:00:02,032 --> 00:00:05,478
In this video we're going to
use Postgres instead of SQLite

3
00:00:05,478 --> 00:00:07,984
in our app so that we don't lose the data

4
00:00:07,984 --> 00:00:10,300
when we deploy again for example,

5
00:00:10,300 --> 00:00:12,818
or when the Dyno goes to sleep.

6
00:00:12,818 --> 00:00:16,941
What we have to of first is
to add Postgres as an add-on.

7
00:00:16,941 --> 00:00:19,717
So we're gonna go over
to the resources section

8
00:00:19,717 --> 00:00:21,994
of our app and we're going to type,

9
00:00:21,994 --> 00:00:25,373
no click on, Find more add-ons.

10
00:00:25,373 --> 00:00:27,463
This takes us to the Heroku add-ons,

11
00:00:27,463 --> 00:00:29,943
also called Heroku Elements
and this has a bunch

12
00:00:29,943 --> 00:00:32,756
of add-ons that we can
add to our application.

13
00:00:32,756 --> 00:00:36,321
There is loads of different
things that we can add.

14
00:00:36,321 --> 00:00:38,475
So if you find anything
interesting that you

15
00:00:38,475 --> 00:00:41,292
wanna work with and maybe
that you know something about

16
00:00:41,292 --> 00:00:44,398
you can definitely instal
it and have a look around.

17
00:00:44,398 --> 00:00:46,863
For example, there's things for MongoDB,

18
00:00:46,863 --> 00:00:50,780
for storing data in Redis
and many more things.

19
00:00:52,495 --> 00:00:55,414
As you can see all the
categories there on the left.

20
00:00:55,414 --> 00:00:59,499
There's even integrations to
send SMS and things like that.

21
00:00:59,499 --> 00:01:02,248
But what we're interested
in is in Postgres,

22
00:01:02,248 --> 00:01:04,501
so we're gonna look for
that and as you can see

23
00:01:04,501 --> 00:01:08,125
there is 157 Postgres related add-ons.

24
00:01:08,125 --> 00:01:12,169
We're interested however on
the first one, Heroku Postgres.

25
00:01:12,169 --> 00:01:15,967
So let's click on that,
and then we're going

26
00:01:15,967 --> 00:01:18,773
to instal Heroku Postgres.

27
00:01:18,773 --> 00:01:22,903
This is free by the way so
we can instal it for free.

28
00:01:22,903 --> 00:01:24,587
We're gonna select the application,

29
00:01:24,587 --> 00:01:27,490
which is gonna be stores.rest.api

30
00:01:27,490 --> 00:01:30,490
and then we're gonna press continue.

31
00:01:32,142 --> 00:01:35,538
This may take while to
do, so if that is the case

32
00:01:35,538 --> 00:01:38,400
just give it a few minutes
and then at the end

33
00:01:38,400 --> 00:01:41,063
we have to choose our plan.

34
00:01:41,063 --> 00:01:43,232
So we can choose Hobby Dev which is

35
00:01:43,232 --> 00:01:44,958
the free plan and as you can see

36
00:01:44,958 --> 00:01:48,627
it goes all the way to $7,000 a month.

37
00:01:48,627 --> 00:01:51,175
So make sure to not select that.

38
00:01:51,175 --> 00:01:53,624
Again, you may have to give
your credit card details

39
00:01:53,624 --> 00:01:58,173
to instal an add-on, but if
you select the free add-on

40
00:01:58,173 --> 00:02:00,800
you will not be charged, okay,

41
00:02:00,800 --> 00:02:04,274
so go ahead and press provision.

42
00:02:04,274 --> 00:02:05,160
There we have it.

43
00:02:05,160 --> 00:02:08,827
Now we have a database
connected to our app.

44
00:02:10,092 --> 00:02:13,157
Now the limitation on this
database is 10 thousand rows.

45
00:02:13,157 --> 00:02:15,677
So we cannot have more
than 10 thousand rows

46
00:02:15,677 --> 00:02:18,754
in our database, distributed
amongst all our tables.

47
00:02:18,754 --> 00:02:21,625
So the total combined number
of rows of all our tables

48
00:02:21,625 --> 00:02:24,327
cannot be more than 10 thousand.

49
00:02:24,327 --> 00:02:27,659
So this is just for developing and working

50
00:02:27,659 --> 00:02:30,001
and checking things out, it's not

51
00:02:30,001 --> 00:02:32,098
for a production application.

52
00:02:32,098 --> 00:02:34,448
If you want a production application

53
00:02:34,448 --> 00:02:36,744
that you're working with a lot of people

54
00:02:36,744 --> 00:02:40,763
and you're maybe making money
from it, you'll have to pay.

55
00:02:40,763 --> 00:02:42,170
Then let's go over to the settings

56
00:02:42,170 --> 00:02:43,507
because there's something that has changed

57
00:02:43,507 --> 00:02:45,902
in our settings now that we have installed

58
00:02:45,902 --> 00:02:48,372
the Postgres add-on.

59
00:02:48,372 --> 00:02:51,640
If you go to Config variables
and reveal your Config Vars

60
00:02:51,640 --> 00:02:53,170
you can see that there's something here

61
00:02:53,170 --> 00:02:57,608
that we didn't write, and
that is a database URL.

62
00:02:57,608 --> 00:03:01,775
Right now, our database
URL is sqlite:///data.db

63
00:03:04,070 --> 00:03:07,570
but now we have another database URL here.

64
00:03:08,409 --> 00:03:09,792
So what we have to do
is we have to make sure

65
00:03:09,792 --> 00:03:13,597
that we use this database
URL in our application.

66
00:03:13,597 --> 00:03:16,714
If not, we will still be using SQLite.

67
00:03:16,714 --> 00:03:20,855
So let's go over to ATOM and we are going

68
00:03:20,855 --> 00:03:24,664
to go over to the app.py
file and we're going

69
00:03:24,664 --> 00:03:26,997
to use the new database URL.

70
00:03:28,695 --> 00:03:31,493
In Heroku the database URL has been added

71
00:03:31,493 --> 00:03:34,101
as an environment variable.

72
00:03:34,101 --> 00:03:37,245
An environment variable is
something that the system,

73
00:03:37,245 --> 00:03:39,417
the computer, or the virtual machine,

74
00:03:39,417 --> 00:03:42,749
in the case of Heroku keeps track of.

75
00:03:42,749 --> 00:03:45,830
That computer is always going to know

76
00:03:45,830 --> 00:03:48,997
the value of the environment variable.

77
00:03:50,051 --> 00:03:52,254
All we have to do is we have to read

78
00:03:52,254 --> 00:03:54,439
that variable from the environment.

79
00:03:54,439 --> 00:03:56,444
In order to do that, at the very top

80
00:03:56,444 --> 00:03:59,740
we're going to import os,
stands for operating system

81
00:03:59,740 --> 00:04:01,271
and that gives us access to

82
00:04:01,271 --> 00:04:04,677
the operating system's
environment variables.

83
00:04:04,677 --> 00:04:07,801
Then down here, before the SQLite we're

84
00:04:07,801 --> 00:04:11,051
gonna type os.environ.get DATABASE_URL.

85
00:04:14,827 --> 00:04:17,326
This is the name of the
variable that Heroku

86
00:04:17,326 --> 00:04:20,790
has created for us, the
environment variable.

87
00:04:20,790 --> 00:04:22,730
What this is gonna do, it is going to go

88
00:04:22,730 --> 00:04:25,405
to the operating system
and it's going to ask it

89
00:04:25,405 --> 00:04:27,507
for this environment variable.

90
00:04:27,507 --> 00:04:31,217
And if we are in Heroku,
then it's going to read

91
00:04:31,217 --> 00:04:34,139
that environment variable
and it's going to connect

92
00:04:34,139 --> 00:04:36,104
to the Postgres app, or rather, it's going

93
00:04:36,104 --> 00:04:39,604
to use that value as the apps Config file.

94
00:04:40,531 --> 00:04:44,444
However, we still wanna be
able to run the app locally

95
00:04:44,444 --> 00:04:47,825
for testing and for developing,
so its also important

96
00:04:47,825 --> 00:04:50,501
that we can use SQLite locally

97
00:04:50,501 --> 00:04:53,418
if the database URL is not defined.

98
00:04:55,061 --> 00:04:57,597
So for example, if we run
the app in our computer

99
00:04:57,597 --> 00:05:00,470
we won't have this
environment variable to find

100
00:05:00,470 --> 00:05:03,064
so we wanna still use SQLite in that case,

101
00:05:03,064 --> 00:05:06,516
and fortunately all that
we have to do is pass

102
00:05:06,516 --> 00:05:10,683
this string as a second
argument to the get method.

103
00:05:12,829 --> 00:05:15,612
So I'm gonna delete this
bracket, put a comma in there

104
00:05:15,612 --> 00:05:19,380
and then put a bracket at the
end and now the get method

105
00:05:19,380 --> 00:05:21,713
is taking in two parameters.

106
00:05:22,776 --> 00:05:26,630
First one, the environment
variable that were going to read

107
00:05:26,630 --> 00:05:30,170
and the second one, the default value.

108
00:05:30,170 --> 00:05:33,194
If this variable is
not found in the system

109
00:05:33,194 --> 00:05:35,095
we're going to use this one instead,

110
00:05:35,095 --> 00:05:37,570
we're gonna use this value instead.

111
00:05:37,570 --> 00:05:40,752
So in our computer we will try to load up

112
00:05:40,752 --> 00:05:44,085
this variable, we would fail
because we've not set it

113
00:05:44,085 --> 00:05:47,560
and then we would use the
SQLite variable instead.

114
00:05:47,560 --> 00:05:49,560
The SQLite code instead.

115
00:05:50,499 --> 00:05:54,666
So save that file and then
go to the requirements.txt

116
00:05:55,998 --> 00:05:58,706
and here we have to add something

117
00:05:58,706 --> 00:06:01,456
which is psycopg2 and press Save.

118
00:06:03,299 --> 00:06:06,646
psycopg2 is a very popular Python library

119
00:06:06,646 --> 00:06:09,229
used to interact with Postgres.

120
00:06:10,985 --> 00:06:13,933
And SQLAlchemy which is
our thing that we're using

121
00:06:13,933 --> 00:06:17,733
to communicate with the
database uses psycopg2

122
00:06:17,733 --> 00:06:21,559
to communicate with Postgres
and so we need to import that.

123
00:06:21,559 --> 00:06:23,605
Even though we are not using it ourselves,

124
00:06:23,605 --> 00:06:25,776
we have to import it in the same way

125
00:06:25,776 --> 00:06:28,276
that we have to instal uwsgi.

126
00:06:29,703 --> 00:06:33,203
So once that's done let's
go over to the terminal

127
00:06:33,203 --> 00:06:37,286
and we're going to do Git
commit am and say added

128
00:06:41,950 --> 00:06:45,700
PostgreSQL environment
variable and psycopg2.

129
00:06:50,027 --> 00:06:52,194
Then we can do a Git push.

130
00:06:53,344 --> 00:06:55,185
Because I've been playing
around with my repository

131
00:06:55,185 --> 00:06:58,062
I have to do -f, but you don't
have to do that yourselves.

132
00:06:58,062 --> 00:07:02,145
Only I have to do that,
you can just do Git push.

133
00:07:03,078 --> 00:07:07,863
Then we can go over to Chrome
and go to our deploy tab

134
00:07:07,863 --> 00:07:11,863
and we can deploy our
app, so we're gonna deploy

135
00:07:12,921 --> 00:07:14,421
that branch there,

136
00:07:17,464 --> 00:07:18,935
and that is deploying,

137
00:07:18,935 --> 00:07:21,518
should only take a few seconds.

138
00:07:25,892 --> 00:07:28,207
That's it then deployed.

139
00:07:28,207 --> 00:07:30,456
So what we're gonna do is go over to

140
00:07:30,456 --> 00:07:33,141
the overview section of our app first

141
00:07:33,141 --> 00:07:35,900
and we're gonna click on Heroku Postgres.

142
00:07:35,900 --> 00:07:38,173
And where this is gonna take
us, this is gonna take us

143
00:07:38,173 --> 00:07:41,332
to our databases, and we've only got one,

144
00:07:41,332 --> 00:07:45,513
so we're gonna press
on the data store there

145
00:07:45,513 --> 00:07:48,093
and here we're gonna be
able to see what data

146
00:07:48,093 --> 00:07:49,976
we've got in our app.

147
00:07:49,976 --> 00:07:52,745
So as you can see we've got
zero connections running.

148
00:07:52,745 --> 00:07:55,531
The limit is 20 simultaneous connections,

149
00:07:55,531 --> 00:07:59,107
we've got zero rows out of a
maximum of 10 thousand rows,

150
00:07:59,107 --> 00:08:00,830
it also tells us our data size

151
00:08:00,830 --> 00:08:03,066
and the number of tables we've got.

152
00:08:03,066 --> 00:08:06,699
Now comes the time to test it,
so let's go over to Postman

153
00:08:06,699 --> 00:08:08,949
and we're going to register

154
00:08:10,552 --> 00:08:13,445
and there we have user
created successfully.

155
00:08:13,445 --> 00:08:16,822
Then we can do some auth,
then we get an access token,

156
00:08:16,822 --> 00:08:20,092
retrieve items for example, and so on.

157
00:08:20,092 --> 00:08:24,259
Now let's go over back to Chrome
and we're going to refresh.

158
00:08:29,072 --> 00:08:30,369
There you have it.

159
00:08:30,369 --> 00:08:35,299
So now we have one row,
which is the registered user.

160
00:08:35,299 --> 00:08:38,708
One active connection, which
is coming from our app.

161
00:08:38,708 --> 00:08:41,958
And three tables, so there you have it.

162
00:08:43,404 --> 00:08:45,712
There's also a bunch of
things you can do down here

163
00:08:45,712 --> 00:08:49,066
such as, get credentials
to access this database

164
00:08:49,066 --> 00:08:52,658
using PGAdmin or your command line.

165
00:08:52,658 --> 00:08:54,842
You can also reset the database
and delete all the data

166
00:08:54,842 --> 00:08:58,509
or of course you can destroy it as well.

167
00:08:58,509 --> 00:08:59,691
So that's everything for this video.

168
00:08:59,691 --> 00:09:03,752
We've looked at how to get
PostgreSQL working with our app,

169
00:09:03,752 --> 00:09:07,255
but remember, we haven't had
to change anything in our code.

170
00:09:07,255 --> 00:09:11,422
SQLite just seamlessly
transfers between SQLite sorry

171
00:09:13,401 --> 00:09:16,805
and PostgreSQL without a problem.

172
00:09:16,805 --> 00:09:18,171
So that's everything for this video

173
00:09:18,171 --> 00:09:19,990
and I'll see you on the next one.

