setup of postgresql, pgadmin and importing data• the two most common clients that you will come...

53
1 Setup of PostgreSQL, pgAdmin and importing data CS3200 Database design (sp18 s2) https://course.ccs.neu.edu/cs3200sp18s2/ Version 2/9/2018

Upload: others

Post on 06-Jun-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

1

SetupofPostgreSQL,pgAdminandimportingdata

CS3200 Databasedesign(sp18 s2)https://course.ccs.neu.edu/cs3200sp18s2/Version2/9/2018

Page 2: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

2

Overview

Thisdocumentcovers2issues:

1)HowtoinstallPostgreSQL:• PostgreSQLisapopularopensourcedatabaseserver.UnlikeSQLite,PostgreSQLisamuch

morefeaturerichdatabasemanagementsystem.WithPostgreSQL,youhave2components,theserverandtheclient.Thisisn'tverydifferentfromyourwebserver-browsermodelwherethebrowserisyourclient:ThewebserverservicesrequestsforfetchingwebpageswhereasadatabaseserverservicesSQLqueriesonadatabase.

• ThisdocumentwillguideyouthroughtheprocessofsettingupPostgreSQLonyourmachine.WhatthismeansisyouwillhavealocallyrunninginstanceofthePostgreSQLserveronyourmachine.

Page 3: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

3

Overview• Justasawebbrowserhelpsmakerequeststoawebserveranddisplaystheresultsofthe

requestviz.awebpage,similarlyadatabaseclienthelpsyoufirequeriesatadatabaseserver(PostgreSQLinourcase),anddisplaystheresultsthatthedatabaseserversendsoverfromprocessingthosequeries.

• ThetwomostcommonclientsthatyouwillcomeacrosswhenusingPostgreSQLare"psql"whichisacommand-lineclientand"pgAdmin"whichisagraphicalclient.

2)HowtoimporttheIMDBdata:• DownloadthebigZIPfilefromouronlinedirectory.Itcontainsalargecollectionofdata

fromtheIMDBmoviewebsite.Thiswillcreateafoldernamedimdb2015,containing6.txtfiles.Youwillusethemtocreateadatabase.

Page 4: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

4

1. Setup PostgreSQL (for MAC)

Page 5: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

5

PostgreSQL

Goto:http://postgresapp.com/

• Gotohttp://postgresapp.com/.• Downloadthelatestreleasedversion(nottheprerelease!).

1)ClickHere toDownload

Page 6: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

6

2)Extractthefileyoujustdownloaded.Typicallythedownloadedfileshouldbeinyourdownloadedfolder.

3)Thepreviousstepwillextractthe“Postgres”application,typicallyinthesamefolder.Lookforafilewithablueelephanticon.

4)DoubleclickonthisfileandPostgreSQLservershouldbeupandrunning.

Page 7: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

7

5)WhilethePostgreSQLserverisupandrunning,youwillseeaniconshowupinyourmenubaratthetop.

6)Clickonthe“Slonik”inthemenubar.

7)Inthemenuthatshowsup,select“OpenPostgres”.

Iflateron,somethingisnotworking,verifythattheserverisrunning

Page 8: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

8

9)Afteryou“OpenPostgres”,thiswindowprovidesyouwithallyourcreateddatabases.

10)Double-clickonthedatabaseyouwanttoworkwith.

Page 9: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

9

11)Afterclickingonthedatabaseyouwanttoworkwith,thePostgrescommandpromptshouldopen,thatshouldlooklike<your_user_name>=#

12)Thisisthepsql clientthatwasmentionedearlierandwhereyouwillenterSQLcommandstointeractwiththedatabaseserver.

13)Tostoptheserver,simplyquittheapplicationbyclickingontheelephanticoninyourmenubarandselectingQuitfromthemenubarthatshowsupunderneaththeicon.

Page 10: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

10

CommandlineCheatsheet

\l listexistingdatabases\c connecttoadatabase\d listtablesindatabase\q disconnectfrompsql

\d<tablename> viewdetailsofatablecreatedatabase<dbname> createDB

Alternatively,startfromterminalwithcommand"psql"

Page 11: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

11

2. Setup PostgreSQL (for Windows)

Page 12: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

12

• Goto http://www.postgresql.org/download/windows/.

1)Clickon“DownloadtheInstaller”

• Double click on the downloaded file. A window will show up that will guide you through the installation.

Page 13: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

13

2)Doubleclickonthedownloadedfile.

Page 14: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

14

3)AwindowwillshowupthatwillguideyouthroughthesetupofPostgreSQL.

4)Click“Next”tocontinuethesetup.

Page 15: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

15

5)TheinstallerwizardwillaskyoutospecifyadirectorywherePostgreSQLshouldbeinstalled.Itisrightifyoustickwiththedefaultoption.Sojustclicknext.

6)Click“Next”tocontinuethesetup.

Page 16: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

16

7)Theinstallerwizardwillthenaskyoutospecifyadatadirectory.Again,itisalrighttostickwiththedefaultoption.

8)Click“Next”tocontinuethesetup.

Page 17: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

17

10)Click“Next”tocontinuethesetup.

9)Youwillnextbepromptedtoenterapasswordforthesuperuser “postgres”.EnterthepasswordandMAKEANOTEOFITasitwillberequiredeverytimeyouwanttoworkwithpostgres.

Page 18: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

18

11)Youwillnextbeaskedtoenteraportnumberfortheservertolistenon.Thedefaultoptionshouldbe5432.Itisalrighttokeepitasitis.

12)Click“Next”tocontinuethesetup.

Page 19: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

19

13)Next,leavethelocaleselectionat“Defaultlocale”

14)Click“Next”tocontinuethesetup.

Page 20: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

20

15)Next,itwillaskyouifyouwanttoinstallStackBuilder.Youcanchecktheboxtoinstallit,howeveryoudon’tneedStackBuilderrightnowandpossiblynotforthedurationofthiscourse.So,youuncheckthebox.

14)Click“Finish”tocompletethesetup.

Page 21: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

21

• Toopenpsql now(whichistheclientwewillusetocreateourdatabaseandinteractwiththedatabaseserver)gotothecommandpromptanddothefollowing:

17)Typethiscommand,asitis.

18)ItwillthenaskforthepasswordyousetduringthePostgreSQLsetup.Note:thatthepasswordyouenterwillnotbevisibletoyou,sojustkeeptypingitCORRECTLY!

19)Onexecutingthepreviouscommand,thepromptshouldchangeandshouldnowlooklike“postgres=#”

Page 22: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

22

• Youarenowinthepsql programwhereyoucanenterqueriestointeractwiththePostgreSQLserver.

• Entering“\q”atthispromptshouldexitthepsql program.Example:postgres=#\q

Page 23: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

23

3. Setup PostgreSQL (for Linux)

Page 24: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

24

Step1:InstallPostgreSQL

HerearetheinstallationstepsonUbuntu(thisinstallationwillalsoworkonanyDebian-baseddistribution):1. Openaterminalwindow.2. Issuethecommandsudo apt-getsintall postgresql.3. Typethesudo passwordnecessarytogiveyouadminrightsandhitEnter.4. Allowapttopickupanynecessarydependencies.

Step2:Changethedefaultuserpassword

Ifyoudon’tfollowthisstep,youwillnotbeabletoadddatabasesandadministerPostgreSQL,andthedatabasewillnotbesecure.

Page 25: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

25

• Here’showtochangethepasswordforthedefaultuser.Theuserinquestionispostgres,andthepasswordischangedlikeso:

1. Openaterminalwindow.2. Issuethecommandsudo passwd postgres.3. Type(andconfirm)thepasswordtobeusedforthisuser.

• Thepostgres userwillbetheonlyuseronyoursystemthatcanopenthePostgreSQLpromptwithoutdefiningadatabase,whichmeanspostgres istheonlyuserwhocanadministerPostgreSQL.

• Totestthis,changetothepostgres userwiththecommandsu - postgres andthenenterthecommandpsql.YoushouldnowbeatthePostgresprompt,whichlookslike:

postgres=#• Allotherusersgainaccesstothepromptlikeso:

psql DB_NAMEWhere,DB_NAMEisthenameofanexistingdatabase.

Page 26: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

26

Step3:ChangethePostgresadminpassword

Theadministratorpasswordmustbeset;otherwise,externalapplicationswillnotbeabletocommunicatewiththedatabase.TochangetheadminpasswordforPostgres,followthesesteps:

1. Openaterminalwindow.2. Changetothepostgres user.3. Logintothepostgres prompt.4. Issuethecommand \passwordpostgres.5. Enter(andverify)thenewpassword.6. Exitthepromptwiththecommand \q.

Page 27: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

27

Step4:Createyourfirstdatabase

Thisiswhereitgetsexciting.Let'screateanewdatabasecalledtestdb.Todothis,followthesesteps:

1. Openaterminalwindow.2. Changetothepostgres user.3. Logintothepostgres prompt.4. Issuethecommand CREATEDATABASEACTOR;.

Page 28: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

28

4. Importing the data

Page 29: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

29

1)Gotooursql directoryonourwebsiteandapagewithlotsoffileswillpop-up.

2)Downloadimdb-cs3200.zipandextractit.

Page 30: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

30

3)Thiswillcreateafoldernamedimdb2015,containing6.txtfiles.

4)Now,inordertocreateadatabase,openPostgres,makesurethattheserverisrunningandconnecttopsql.

Page 31: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

31

5)Oncetheserverisrunningandconnectedtopsql,youshouldseesomethinglike<your_user_name>=#asyourprompt.

6)Tocreateanewdatabase,enterthefollowingcommands:priyalmittal=#CREATEDATABASEimdb;priyalmittal=#\cimdb

Yourpromptwillthenswitchtoimdb=#

Page 32: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

32

7)Youcannowusethefollowingcommandtocreateatable:imdb=#CREATETABLEtable_name(……);

YouwillhavetodecideontheappropriatedatatypesJ

8)Chooseappropriatetypesforeachcolumndspecifyallkeyconstraintsasdescribedhere.(partofyourfirsthomework)

Page 33: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

33

9)Oncetablesarecreated,youcanusethepsql command\copy,toimportdatafromthecorrespondingtextfiles.

10)Thiscommandimportsdatafromthe“actor.txt”file,whichistobementionedalongwithitscompletepath,intotheACTORtable.

11)Thedelimiterspecifiesowthefieldsareseparated,whereasthelastpartofthestatementspecifiesthattheemptystringshouldbeassignedasaNULLvalue.

NOTE:The\copycommandmaytakeafewminutestocompleteforsometables(likelylongerthan10minforcasts).Theexactspeedoftheseoperationsmayvarydependingonyourmachine.

Page 34: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

34

NOTE:Youmaygetanerrorwhenrunningthecopycommandthatlookssimilartothis:

ERROR:invalidbytesequenceforencoding"UTF8":0xc30x7c.

Thatmeansthatpsql issettoanencodingthatdoesnotmatchthefile.Trychangingtheencoding,andtrycopyingagain.

imdb=# set client_encoding to 'latin1';

Page 35: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

35

5. Creating the .sql files

Page 36: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

36

3)Pastethequeriesintoatexteditor,ASITIS,withoutmakinganychangestothefont,color,size,etc.andsavethefilewiththeextensionof.sql

2)Youmayaddcommentsintothefileasmentionedhere.

1)Copythequeriesyouwroteinthecommandpromptasitis,WITHOUTthe“imdb=#”.

Page 37: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

37

PLEASENOTE:Addinganyextrasymbolsintoyour.sql file,suchas/ attheendofeachlineoranywhereelse,#,$,etc.mayresultintoerrorswhichwouldn’tletyourfilegetexecuted.Theonlyaddedtextpermittedisanylinethatispreceeded by"--":-- isusedtoaddcommentstothefile.

Page 38: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

38

6. Environment Variables Setup

Page 39: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

39

• Ifyouencountertheerror:“psql notrecognizedasaninternalorexternalcommand”issueforPostgreSql inWindows,thenyouaresupposedtofollowthesesteps:

2)Clickon“EnvironmentVariables”.

1)Whenyousearchyourmachinewith“Editthesystemenvironmentvariable”,thiswindowshowsup.

Page 40: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

40

3)Double-clickonthehighlightedpath.

Page 41: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

41

4)Clickon”New”andaddthehighlightedpathbelow.Ifyoufollowedthedefault,itshouldbethesame.Otherwise,youcancheckforthebin folderinthePostgreSQL folderandcopyitspath.

5)Clickon“OK”andrestartcommandprompt.Thisshouldhopefullysolvetheissue.

Page 42: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

42

7. How to import an SQL file

Page 43: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

43

• Youcanrunthechinooksql fileusingthefollowingcommandinpostgres prompt:

\i ‘319- Chinook– PostgreSql.sql’;

• \i filename.sql isthecommand.• Also,usethecompletepathofthefile

Example:

\i ‘/Users/……./foldername/319– Chinook– PostgreSql.sql’;

• YoucanalsojustcopyandpastethefileintoPgAdmin.

Page 44: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

44

8. PgAdmin (version 3 or 4)

Page 45: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

45

• pgAdmin isavisualclientforyourpostgres database• Youcaneitherusethelatestversion4ortheolderversion3• Noticethat– accordingtomanyusers– version4ismorecumbersomethanversion3.Wethusrecommendthatyoudownloadversion3insteadofversion4:https://www.pgadmin.org/download/

• Youmaygetsomeerrorswhenfirstinstallingwhenworkingwithpostgres 10,buttheyshouldnotpreventyoufromusingit

Page 46: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

46

ConnectingtoPostgreSQLdatabasewithpgAdmin 3(afterpgAdmin 3issuccessfullyinstalled)

1)OpenPostgres,suchthattheslonik iconshouldappearonthetopofyourdesktop.

2)Doubleclickontheparticulardatabase,forwhomtheconnectionwithpgAdmin 3istobeestablished,toopenitscommandprompt.

Page 47: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

47

3)Afterclickingonthedatabase,itscorrespondingcommandpromptshouldopenup.

4)WritethisstatementasitiswiththePASSWORDofyourchoice.

NOTE:(i)Please,notethepasswordsomewhereasyouwillneedtoenteritintopgAdmin,everytimeyouwanttorunqueriesforadatabase.

(ii)REMEMBERTOADDTHESEMI-COLON.

Page 48: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

48

Now,tosetupanewserveconnectioninpgAdmin,openpgAdmin and:

5)GotoFile->AddServer

6)EnsurethattheNameandHostissettolocalhostandthePortto5432(unlessyouhaveconfigurePostgreSQLforadifferentport)

7)EnterthePasswordyoujustsetforthedatabaseinthePostgrescommandprompt.

RESTALLREMAINSTHESAME!

Page 49: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

49

Toconnecttotheserver: 8)Double-clickontheserveritem.

9)Providethepasswordwhenprompted.Youprobablywanttostoreit

Page 50: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

50

10)ClickOKeverytimeyougetapop-up.YouwillhavetoclickOKseveraltimes.

Page 51: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

51

11)TochecktheTablesintheDatabase,expandtheparticularDatabase.

12)ExpandtheTablestocheckwhatalltablesarepresentinthedatabase.

RightclickonDatabasesinordertocreateandaddanewdatabase

Page 52: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

52

13)ToopentheSQLwindowtorunqueriesoveraparticulardatabase,clickonthedatabase.

14)ThenclickonthisSQLbutton.

Page 53: Setup of PostgreSQL, pgAdmin and importing data• The two most common clients that you will come across when using PostgreSQL are "psql" which is a command-line client and "pgAdmin"

53

15)Thisiswhereyoucanrunqueriesforaparticulardatabase.

ThisindicatestheDATABASEthatyouarecurrentlyworkingon.

Afterwritingaquery,pressthisRUNbuttontoexecutethequery.OrjusthitF5