20
CS 122A Fall 2021 M. Carey Setup PostgreSQL 1 Resources Here are some links for you to start the wonderful journey of exploring the PostgreSQL world! 1.1 PostgreSQL Download and Installation: https://www.postgresql.org/download/ 1.2 PostgreSQL documentation: https://www.postgresql.org/docs/current/ We provide example installation guidance for Mac, Windows and Linux users in the following section 2. Please refer to the links in section 1.1 if you find those steps do not work for your OS version. After installation, please continue from section 3. 2 Installation guidelines Disclaimer: The following steps are tested only on our own computers with corresponding OS versions. We do NOT guarantee the steps will work perfectly on your own computer too. Please be sure to search and read resources on the internet before you post any environment related questions on Piazza. 2.1 Mac OSX users: These steps were tested on a MacBook Pro Intel model running MacOS BigSur v11.6. We take the current latest version 13.4 (As of speaking, the latest version 14 was just released on Sep/30.) as the example version used in the following guidance. You don’t have to use this version, but we will use version 13.4 as our testing bed for our assignments and instructions. (1) Download installation package from here: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads Select Download button from Version 13.4 and Mac OS X 1

1 Resources Setup PostgreSQL

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

CS 122A Fall 2021 M. Carey

Setup PostgreSQL

1 ResourcesHere are some links for you to start the wonderful journey of exploring the PostgreSQL world!

1.1 PostgreSQL Download and Installation:https://www.postgresql.org/download/

1.2 PostgreSQL documentation:https://www.postgresql.org/docs/current/

We provide example installation guidance for Mac, Windows and Linux users in thefollowing section 2. Please refer to the links in section 1.1 if you find those steps do notwork for your OS version. After installation, please continue from section 3.

2 Installation guidelinesDisclaimer: The following steps are tested only on our own computers withcorresponding OS versions. We do NOT guarantee the steps will workperfectly on your own computer too. Please be sure to search and readresources on the internet before you post any environment relatedquestions on Piazza.

2.1 Mac OSX users:These steps were tested on a MacBook Pro Intel model running MacOS BigSur v11.6.

We take the current latest version 13.4 (As of speaking, the latest version 14 was just releasedon Sep/30.) as the example version used in the following guidance. You don’t have to use thisversion, but we will use version 13.4 as our testing bed for our assignments and instructions.

(1) Download installation package from here:https://www.enterprisedb.com/downloads/postgres-postgresql-downloadsSelect Download button from Version 13.4 and Mac OS X

1

CS 122A Fall 2021 M. Carey

(2) Open file “postgresql-13.4-1-osx.dmg” and double-click the application“postgresql-13.4-1-osx”.

(3) “Next” with default values.

2

CS 122A Fall 2021 M. Carey

3

CS 122A Fall 2021 M. Carey

Choose your own password and memorize it.In my case, I assume the password is also: postgres

4

CS 122A Fall 2021 M. Carey

If you use an English environment computer like me, Default is good, otherwise, pleasebe careful that it should support English character encodings. We will keep that in mind, if wemeet any character encoding issues, we might revisit this setting in the future.

5

CS 122A Fall 2021 M. Carey

6

CS 122A Fall 2021 M. Carey

No need to “Launch Stack Builder at exit”.-- Done --

2.2 Windows UsersThese steps were tested on Windows 10 Home Version 21H1 64-bit.

We take the current latest version 13.4 (As of speaking, the latest version 14 was just releasedon Sep/30.) as the example version used in the following guidance. You don’t have to use thisversion, but we will use version 13.4 as our testing bed for our assignments and instructions.

(1) Download installation package from here:https://www.enterprisedb.com/downloads/postgres-postgresql-downloadsSelect Download button from Version 13.4 and Windows x86-64

(2) Open file “postgresql-13.4-2-windows-x64.exe”(3) “Next” with default values.

7

CS 122A Fall 2021 M. Carey

8

CS 122A Fall 2021 M. Carey

9

CS 122A Fall 2021 M. Carey

Choose your own password and memorize it.

10

CS 122A Fall 2021 M. Carey

11

CS 122A Fall 2021 M. Carey

If you use an English environment computer like me, Default is good, otherwise, pleasebe careful that it should support English character encodings. We will keep that in mind, if wemeet any character encoding issues, we might revisit this setting in the future.

12

CS 122A Fall 2021 M. Carey

13

CS 122A Fall 2021 M. Carey

No need to “Launch Stack Builder at exit”.-- Done --

2.3 Linux Users (Ubuntu)These steps were tested on an AWS t2.micro virtual machine running Ubuntu Serverv18.04.

We take the current latest version 13.4 (As of speaking, the latest version 14 was just releasedon Sep/30.) as the example version used in the following guidance. You don’t have to use thisversion, but we will use version 13.4 as our testing bed for our assignments and instructions.

(1) Open a terminal window (make sure you have sudo privilege). Run the followingcommands sequentially.

(2) Create the file repository configuration:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt$(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

(3) Import the repository signing key:

14

CS 122A Fall 2021 M. Carey

wget --quiet -O -https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-keyadd -

(4) Update the package lists:

sudo apt-get update

(5) Install PostgreSQL-13:

sudo apt-get -y install postgresql-13

(6) Setup password for OS user “postgres”

sudo passwd postgres

Choose your own password and memorize it. (Here I use “postgres” as password)

(7) Login to OS as user “postgres”

su - postgres

Password is what you just set up in (6).

(8) Login to PostgreSQL database and change the password for PostgreSQL user“postgres”. (Note: this user “postgres” is the database user, not the OS user in (6))

psql

postgres=# ALTER USER postgres PASSWORD 'postgres';

postgres=# \q

Choose your own password and memorize it. (Here I use “postgres” again!)

(9) Change authentication methods to password

vi /etc/postgresql/13/main/pg_hba.conf

Change the following line from “peer” to “md5”

local all postgrespeer

local all allpeer

Exit OS user “postgres” back to your own SUDO privilege user.

exit

15

CS 122A Fall 2021 M. Carey

(10) Restart PosgreSQL.

sudo systemctl restart postgresql

Now you can use psql to login to PostgreSQL from any OS user but using the databaseuser “postgres” with password. See option-2 in section 3.

-- Done --

3 Use psql to connect to your PostgreSQL databaseOnce you have PostgreSQL setup, open the psql command line tool to connect to thePostgreSQL database:

Option-1 (Mac OSX users who followed section 2.1):These steps were tested on a MacBook Pro Intel model running MacOS BigSur v11.6.Go to “Finder -> Applications -> PostgreSQL 13”, double click “SQL Shell (psql)”.

The password is what you set in section 2 with Username as postgres.

Option-2 (Other users):Open a Terminal, run

psql -d postgres -U postgres -W

The password is what you set in section 2 with Username as postgres.

16

CS 122A Fall 2021 M. Carey

4 Send your first SQL queryEnter the following query to psql:

select version();

You should see something similar to this:

psql (13.4)Type "help" for help.

postgres=# select version();version

-------------------------------------------------------------------------------------------------------------PostgreSQL 13.4 on x86_64-apple-darwin, compiled by Apple clangversion 11.0.3 (clang-1103.0.32.59), 64-bit(1 row)

postgres=#

5 Populate the “Sailors-Reserves-Boats” dataset

Copy & paste the following SQL queries to psql terminal, then enter.

DROP TABLE IF EXISTS Boats;CREATE TABLE Boats (bid int NOT NULL,bname varchar(45),color varchar(15),PRIMARY KEY (bid)

);

INSERT INTO BoatsVALUES (101,'Interlake','blue'),

(102,'Interlake','red'),(103,'Clipper','green'),

17

CS 122A Fall 2021 M. Carey

(104,'Marine','red');

DROP TABLE IF EXISTS Reserves;

CREATE TABLE Reserves (sid int,bid int,date date

);

INSERT INTO Reserves VALUES (22,101,'1998-10-10'),(22,102,'1998-10-10'),(22,103,'1998-10-08'),(22,104,'1998-10-07'),(31,102,'1998-11-10'),(31,103,'1998-11-06'),(31,104,'1998-11-12'),(64,101,'1998-09-05'),(64,102,'1998-09-08'),(74,103,'1998-09-08'),(NULL,103,'1998-09-09'),(1,NULL,'2001-01-11'),(1,NULL,'2002-02-02');

DROP TABLE IF EXISTS Sailors;

CREATE TABLE Sailors (sid int,sname varchar(45),rating int,age decimal(5,1),PRIMARY KEY (sid)

);

INSERT INTO Sailors VALUES (22,'Dustin',7,45.0),(29,'Brutus',1,33.0),(31,'Lubber',8,55.5),(32,'Andy',8,25.5),(58,'Rusty',10,35.0),(64,'Horatio',7,35.0),(71,'Zorba',10,16.0),(74,'Horatio',9,35.0),(85,'Art',4,25.5),(95,'Bob',3,63.5),(101,'Joan',3,NULL),

18

CS 122A Fall 2021 M. Carey

(107,'Johannes',NULL,35.0);

Now, you can answer the question “What are the names of the sailors who reserved boat#103?” by issuing the following SQL query:

SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND R.bid=103;

You should see the following results:

sname---------DustinLubberHoratio(3 rows)

Congratulations! You are good to go with your PostgreSQL journey!

6 [Optional] Use pgAdmin to connect/manage yourPostgreSQL databaseThese steps were tested on a MacBook Pro Intel model running MacOS BigSur v11.6.Go to “Finder -> Applications -> PostgreSQL 13”, double click “pgAdmin 4”.Setup a master password. (e.g., we use “postgres” again)On the left side panel, expand “Servers”, you will see all installed PostgreSQL servers runningon your machine. For my case, only the installed PostgreSQL 13 is there. It asks for thepassword of user “postgres” to establish connection to the server. (In my case,password=”postgres” again)

19

CS 122A Fall 2021 M. Carey

Then you can see all elements inside the server on the left panel. In our case, you should atleast be able to expand “Databases -> postgres -> Schemas -> Tables” and see the three tables(i.e., boats, reserves and sailors) we created in section 5.

Note: pgAdmin is a GUI tool to query and manage PostgreSQL databases. We introduce it herefor your convenience in case you may prefer a GUI interface than a command line interface.However, it is not required for the course work where psql is sufficient. I will leave theexploration of other features of the tool to you. Good luck!

20