1
00:00:00,320 --> 00:00:02,666
- [Instructor] Hi and
welcome back to the course,

2
00:00:02,666 --> 00:00:03,957
in this video we're going to be

3
00:00:03,957 --> 00:00:06,418
installing PostgreSQL on our server.

4
00:00:06,418 --> 00:00:10,585
If you are on Mac, or any
other Unix operating system,

5
00:00:12,240 --> 00:00:16,297
then you can connect to the
server via your terminal.

6
00:00:16,297 --> 00:00:18,026
So what I've done here on the Mac is

7
00:00:18,026 --> 00:00:20,500
open up a terminal and I'm
going to connect to the server

8
00:00:20,500 --> 00:00:21,967
from here, if you're on Windows,

9
00:00:21,967 --> 00:00:25,967
or if you don't have any
SSH options on Windows,

10
00:00:26,846 --> 00:00:29,237
which you won't normally, then you can

11
00:00:29,237 --> 00:00:31,737
connect via the web interface.

12
00:00:33,514 --> 00:00:35,639
In both cases it's the same thing,

13
00:00:35,639 --> 00:00:37,258
so you'll have to do the same commands

14
00:00:37,258 --> 00:00:39,185
and we're connected to the terminal.

15
00:00:39,185 --> 00:00:41,763
If you're on Windows, you
can connect via the website

16
00:00:41,763 --> 00:00:43,332
using the access console option

17
00:00:43,332 --> 00:00:46,494
that we showed in the last video.

18
00:00:46,494 --> 00:00:48,666
So what we're gonna do
is, type the command,

19
00:00:48,666 --> 00:00:52,833
ssh and then, root, @ and
then the server address.

20
00:00:57,264 --> 00:00:59,144
Now, here I'm being asked for my password,

21
00:00:59,144 --> 00:01:00,992
but if it's the first time you log in,

22
00:01:00,992 --> 00:01:03,654
it will ask you about authentication,

23
00:01:03,654 --> 00:01:06,065
if you wanna trust that this
server is who it says it is

24
00:01:06,065 --> 00:01:08,117
and in that case, you should type, yes

25
00:01:08,117 --> 00:01:09,511
and it will let you connect.

26
00:01:09,511 --> 00:01:12,165
Okay then type in your password and

27
00:01:12,165 --> 00:01:15,787
even if, you don't see
anything appearing there,

28
00:01:15,787 --> 00:01:18,856
I've typed in my password,
the password has been typed,

29
00:01:18,856 --> 00:01:20,800
just press enter at the end and if you put

30
00:01:20,800 --> 00:01:22,686
the correct password
in, it will let you in.

31
00:01:22,686 --> 00:01:25,083
Even though nothing's
appearing while you type,

32
00:01:25,083 --> 00:01:28,343
that's just for security purposes.

33
00:01:28,343 --> 00:01:31,426
Once that's done, you are essentially

34
00:01:32,496 --> 00:01:36,663
logged in as the root user,
the root user in Unix,

35
00:01:37,598 --> 00:01:41,308
such as, Ubuntu, CentOS, Mac and so on,

36
00:01:41,308 --> 00:01:43,250
the root user is very important because

37
00:01:43,250 --> 00:01:47,417
it is the super user, it
has power to do anything.

38
00:01:48,366 --> 00:01:51,105
Okay, so we're going to
learn, in a later video,

39
00:01:51,105 --> 00:01:54,049
how to avoid logging in as the root user.

40
00:01:54,049 --> 00:01:55,970
But for now, we're gonna stick with

41
00:01:55,970 --> 00:01:58,493
the root user for just a moment.

42
00:01:58,493 --> 00:02:01,160
We're going to do apt-get update

43
00:02:04,312 --> 00:02:07,022
and what this is going to
do, it's going to tell apt,

44
00:02:07,022 --> 00:02:09,972
the Ubuntu package manager, to find out

45
00:02:09,972 --> 00:02:12,850
if there are any updates
available for anything

46
00:02:12,850 --> 00:02:15,521
we've got installed in the server.

47
00:02:15,521 --> 00:02:18,053
It's not going to actually
update the package,

48
00:02:18,053 --> 00:02:20,228
it's just going to find out if there are

49
00:02:20,228 --> 00:02:23,051
any updates available and there are a few

50
00:02:23,051 --> 00:02:24,831
and because I've run this before,

51
00:02:24,831 --> 00:02:26,418
so you can see it says up here,

52
00:02:26,418 --> 00:02:29,085
that 21 packages can be updated.

53
00:02:31,004 --> 00:02:32,580
While we can update these packages,

54
00:02:32,580 --> 00:02:34,170
it's not imperative
that we do so right now,

55
00:02:34,170 --> 00:02:36,832
so what we're gonna do is
we're gonna instal PostgreSQL

56
00:02:36,832 --> 00:02:39,045
and fortunately, in Ubuntu, we can

57
00:02:39,045 --> 00:02:41,276
do so via the same package manager,

58
00:02:41,276 --> 00:02:44,623
so we're gonna do apt-get instal

59
00:02:44,623 --> 00:02:47,322
and then, PostgreSQL
is one of the packages

60
00:02:47,322 --> 00:02:50,277
we want to instal and
then separated by a space,

61
00:02:50,277 --> 00:02:52,802
we can put another package
that we also want to instal,

62
00:02:52,802 --> 00:02:55,558
so we're gonna instal two
packages simultaneously.

63
00:02:55,558 --> 00:02:58,975
The second package is PostgreSQL-contrib.

64
00:03:01,661 --> 00:03:03,592
So we're gonna instal those two

65
00:03:03,592 --> 00:03:05,763
and here at the end, it asks
you if you want to continue

66
00:03:05,763 --> 00:03:07,566
because we're about to download stuff

67
00:03:07,566 --> 00:03:10,012
and that's gonna use up our discs space.

68
00:03:10,012 --> 00:03:13,736
These servers, the cheap
servers, $5 a month,

69
00:03:13,736 --> 00:03:16,814
have 20 gigabytes of
storage and here we're

70
00:03:16,814 --> 00:03:20,451
gonna use 20 megabytes,
so that's not a lot.

71
00:03:20,451 --> 00:03:23,994
In Unix, whenever you're asked a question,

72
00:03:23,994 --> 00:03:27,834
there maybe a capital
letter and a lowercase one,

73
00:03:27,834 --> 00:03:30,720
uppercase letter and a lowercase letter.

74
00:03:30,720 --> 00:03:33,403
The uppercase letter is the default,

75
00:03:33,403 --> 00:03:37,139
so if we just press enter,
that is if we typed, Y.

76
00:03:37,139 --> 00:03:40,145
So we can just press enter
and that accepts the default,

77
00:03:40,145 --> 00:03:42,126
because that's the
uppercase letter up there

78
00:03:42,126 --> 00:03:45,626
and then it's installing all of the stuff.

79
00:04:07,207 --> 00:04:09,074
Okay, so we've installed PostgreSQL

80
00:04:09,074 --> 00:04:12,571
and as you can see, up
here, we've created a new

81
00:04:12,571 --> 00:04:16,008
PostgreSQL cluster, a
new server that's running

82
00:04:16,008 --> 00:04:20,175
PostgreSQL and it's running
locally, on port 5432,

83
00:04:21,101 --> 00:04:24,518
which is the default port for PostgreSQL.

84
00:04:27,576 --> 00:04:30,080
Now, when we instal PostgreSQL,

85
00:04:30,080 --> 00:04:33,343
it also creates another
user on our server.

86
00:04:33,343 --> 00:04:35,586
So right now, we've got the root user,

87
00:04:35,586 --> 00:04:37,794
which is what we're logged in as.

88
00:04:37,794 --> 00:04:39,903
But when we instal PostgreSQL, it

89
00:04:39,903 --> 00:04:43,286
adds another user to our server.

90
00:04:43,286 --> 00:04:45,698
That user is the one who owns PostgreSQL,

91
00:04:45,698 --> 00:04:47,948
it owns the PostgreSQL installation.

92
00:04:47,948 --> 00:04:51,564
So we're going to change
over to that user,

93
00:04:51,564 --> 00:04:53,748
we're gonna say, instead
of being logged in

94
00:04:53,748 --> 00:04:55,849
as the root user, we're gonna swap over

95
00:04:55,849 --> 00:04:58,278
and be logged in as the PostgreS user.

96
00:04:58,278 --> 00:05:02,445
What we have to do, is type
in, sudo -i -u postgres,

97
00:05:05,413 --> 00:05:08,431
the postgres user is the
one that has been created

98
00:05:08,431 --> 00:05:10,336
and what we're seeing here, is

99
00:05:10,336 --> 00:05:13,336
we want to log in as this user here.

100
00:05:16,030 --> 00:05:17,918
And as you can see, now
things have changed,

101
00:05:17,918 --> 00:05:22,178
instead of root @ our server,
we see postgres @ our server

102
00:05:22,178 --> 00:05:25,261
because we are now the postgres user.

103
00:05:26,920 --> 00:05:29,131
What this means is that we can now type,

104
00:05:29,131 --> 00:05:33,298
psql and this connects to
the PostgresSQL database.

105
00:05:35,912 --> 00:05:38,844
Notice that we don't have any tables

106
00:05:38,844 --> 00:05:43,011
or anything like that yet
so, there's no data in here.

107
00:05:45,090 --> 00:05:48,840
The Postgres user has
connected to PostgreSQL

108
00:05:49,870 --> 00:05:52,746
and is connected to one specific database

109
00:05:52,746 --> 00:05:55,140
and it has created a database.

110
00:05:55,140 --> 00:05:58,862
Every user in postgres,
automatically connects

111
00:05:58,862 --> 00:06:03,029
to a postgres database, with
the same name as the user.

112
00:06:04,513 --> 00:06:06,771
So here, what we've done is we've created

113
00:06:06,771 --> 00:06:09,938
a PostgreSQL database called, postgres

114
00:06:11,080 --> 00:06:13,546
and when we run the psql command,

115
00:06:13,546 --> 00:06:16,055
we connect to the database that

116
00:06:16,055 --> 00:06:18,562
is called the same as our user.

117
00:06:18,562 --> 00:06:21,934
So effectively, we've connected
to the postgres database

118
00:06:21,934 --> 00:06:23,963
and as you can see here, we've

119
00:06:23,963 --> 00:06:25,636
connected to the postgres database.

120
00:06:25,636 --> 00:06:29,053
At any point, we can press the backslash,

121
00:06:31,018 --> 00:06:34,529
which is that symbol
there and then, conninfo

122
00:06:34,529 --> 00:06:38,129
and that tells us some connection info.

123
00:06:38,129 --> 00:06:40,324
You are connected to database postgres,

124
00:06:40,324 --> 00:06:43,008
as you can see I just told
you we created that database.

125
00:06:43,008 --> 00:06:47,216
As the user, postgres, which
is the user we logged in as,

126
00:06:47,216 --> 00:06:49,885
via the socket /var/run/postgresql

127
00:06:49,885 --> 00:06:52,510
in port 5432, which is the default.

128
00:06:52,510 --> 00:06:56,677
So hopefully, up to now,
everything kinda make sense

129
00:06:57,984 --> 00:07:01,095
and, what we have done now, is we have

130
00:07:01,095 --> 00:07:02,890
connected to the PostgreSQL server,

131
00:07:02,890 --> 00:07:05,374
we've installed PostgreSQL and in here,

132
00:07:05,374 --> 00:07:07,999
we can do things like
run Postgres commands,

133
00:07:07,999 --> 00:07:12,785
create table, items and blah
blah blah, if we want to.

134
00:07:12,785 --> 00:07:15,317
However, we know that
our application is gonna

135
00:07:15,317 --> 00:07:17,944
do that for us, so we
don't have to worry about

136
00:07:17,944 --> 00:07:21,453
running sql commands
ourselves, which is good.

137
00:07:21,453 --> 00:07:25,407
At any point, we can also type, \q

138
00:07:25,407 --> 00:07:28,593
and that exits the psql process,

139
00:07:28,593 --> 00:07:32,529
it leaves the database and
goes back to the Unix terminal

140
00:07:32,529 --> 00:07:35,529
and at any moment, I can press, exit

141
00:07:37,010 --> 00:07:40,593
and that will log out
off the postgres user

142
00:07:41,654 --> 00:07:44,946
and go back to being the root user.

143
00:07:44,946 --> 00:07:48,527
Okay, so what we did is
we logged in as postgres,

144
00:07:48,527 --> 00:07:50,667
we accessed the postgres database

145
00:07:50,667 --> 00:07:54,710
and then we've logged out and
we are back to being the root.

146
00:07:54,710 --> 00:07:57,339
In the next video, we're
going to be creating a user

147
00:07:57,339 --> 00:07:59,999
so that we don't have to log in as root

148
00:07:59,999 --> 00:08:02,980
and so we don't have to
use the postgres user

149
00:08:02,980 --> 00:08:05,430
to access PostgreSQL, I'm
going to create another user

150
00:08:05,430 --> 00:08:09,090
with access, so I'll see
you in the very next video.

