Skip to content

Postgresql Setup and Light Tutorial for Mac Users (2 methods for setup) #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3093f43
Link in tech stack
michael-j-rubenstein Mar 21, 2023
3974067
Created new file for postgres instructions
michael-j-rubenstein Mar 21, 2023
558505c
added first section
michael-j-rubenstein Mar 21, 2023
d4cf9b6
added more updates to the first section
michael-j-rubenstein Mar 21, 2023
620fb5a
added an images folder
michael-j-rubenstein Mar 21, 2023
d95fc75
Add files via upload
michael-j-rubenstein Mar 21, 2023
3e4d4c4
Delete file-temp.md
michael-j-rubenstein Mar 21, 2023
2642310
added image to section 1
michael-j-rubenstein Mar 21, 2023
12b63ad
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
0378471
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
8ace1de
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
194acd5
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
2c530d7
Add files via upload
michael-j-rubenstein Mar 21, 2023
afc5868
Add files via upload
michael-j-rubenstein Mar 21, 2023
99ed0f7
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
eaf414f
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
708c10d
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
b371722
finished section 3
michael-j-rubenstein Mar 21, 2023
5d55242
Finished section 4
michael-j-rubenstein Mar 21, 2023
f8a8c0f
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
fdec32b
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
fba7344
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
3959a53
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
826625e
added more images
michael-j-rubenstein Mar 21, 2023
f17afec
Update Setup_and_Run_Postgresql_Locally_Mac.md
michael-j-rubenstein Mar 21, 2023
bcc66f6
Update and rename Setup_and_Run_Postgresql_Locally_Mac.md to Postgres…
michael-j-rubenstein Mar 21, 2023
1c9ae34
Update Tech_Stacks.md
michael-j-rubenstein Mar 21, 2023
fd22509
Rename Postgresql_Setup_and_Tutorial_Mac.md.md to Postgresql_Setup_an…
michael-j-rubenstein Mar 21, 2023
1a0337e
Update Tech_Stacks.md
michael-j-rubenstein Mar 21, 2023
7556b2f
Merge branch 'main' into main
michael-j-rubenstein Mar 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Topics/Tech_Stacks.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## Tech Stacks

### [Learning MySQL for databases](./Tech_Stacks/Learning_MySQL.md)
### [React Resources](./Tech_Stacks/React.md)


### [Postgresql Setup and Light Tutorial for Mac Users (2 Methods)](./Tech_Stacks/Postgresql_Setup_and_Tutorial_Mac.md)
### [React Resources](./Tech_Stacks/React.md)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Topics/Tech_Stacks/Images/postgres_step1_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions Topics/Tech_Stacks/Postgresql_Setup_and_Tutorial_Mac.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Postgresql local setup instructions and tutorial for Mac Users

## Description
This document provides instructions on how to setup postgresql locally on your mac using two methods, the first being through pgAdmin4 and the second being through the psql shell. Below please find an overview of what is covered in this document:
1. [Downloading Postgresql and PgAdmin](#one)
2. [Creation of a user in Postgresql](#two)
3. [Creation of a database with owner of "user" in Postgresql](#three)
4. [Instructions on how to see tables in your database](#four)
5. [Instructions on how to create tables in your database](#five)
6. [Instructions on how to delete tables in your database](#six)

<a name="one"/>

## Downloading Postgresql and PgAdmin
To be able to setup Postgresql locally we first need to download it. First we check to see if we already have Postgresql installed by running: ```psql --version``` in the command line. This would return the version of Postgres if you have it installed already, if you already have Postgresql installed and don't want to use PgAdmin then please move onto the next section. If not continue reading.

To Install Postgresql we will be using an interactive installer that can be found on [Postgres's website](https://www.postgresql.org/download/macosx/). Click the "Download the installer" link under the **Interactive installer by EDB** section. (**NOTE: The installer is by EDB, and will redirect you to another site**)

After arriving to the site you can click the download icon for Mac OS X for your desired version of Postgresql. This will install the interactive installer on your computer, which includes postgres and pgAdmin4. Image below:

<img src="https://github.com/michael-j-rubenstein/learning-software-engineering.github.io/blob/main/Topics/Tech_Stacks/Images/postgres_step1_1.png" width=500 />

Now you should have a ```.dmg``` file in the download folder, open it and follow the steps in their installer. **NOTE:** Make sure you select PostgreSQL Server and select pgAdmin and/or Command Line Tools in the "select Components" page based on your preference. I suggest to select all of the options as they are all pretty useful.

<img src="https://github.com/michael-j-rubenstein/learning-software-engineering.github.io/blob/2c530d7293dbf5234d45df54a4ac4c18d7e0a961/Topics/Tech_Stacks/Images/postgres_interactive_installer.png" width = 300 />

The port number of the server can be anything, but default is 5432.

<a name="two" />

## Creating a user in Postgresql
Now that you have Postgresql successfully installed, here are steps to create a new user:

### Using PgAdmin4
1. Launch up PgAdmin4
2. Insert the password you used in the installer in the previous section.
3. In the tab on the left side of the screen, go to ```Servers > Your wanted server (Default is PostgreSQL 15) > Login/Group Roles```
4. Right click and select the ```Create > Login/Group Role``` option
5. You can insert the information you want for the User, click the Save option and you're done!

Note that you area also able to set a password for the user and even superuser priviledges.

<img src="https://github.com/michael-j-rubenstein/learning-software-engineering.github.io/blob/main/Topics/Tech_Stacks/Images/postgres_pgadmin_user_setup.png" width=500 />

### Using Postgresql Shell
1. Open up the Mac Terminal
2. Run ```sudo -u postgres psql``` to open up the Postgresql Shell using the default postgres user
3. Insert the password you used in the installer in the previous section
4. If successful you should see that the prompt for the terminal looks something like this: ```postgres=#```
5. Now to create a new user write ```CREATE USER your_users_name;```
6. A prompt will show to insert password for the user, after setting the password you're done!

Note that if you want to create a superuser you can use ```CREATE USER your_users_name SUPERUSER;``` instead in step 5.

More information and additional methods can be found here: [How to create a Postgres User](https://phoenixnap.com/kb/postgres-create-user)

<a name="three" />

## Creating a database in Postgresql
Now that you have a custom user, you can now create a database!

### Using PgAdmin4
1. Launch the app and insert your passwords
2. In the tab on the left side of the screen go to ```Servers > Your wanted server (Default is PostgreSQL 15) > Databases```
3. Right click and select the ```Create > Database...``` option
4. Insert the name of the database in ```Database``` field
5. If you want to change the Owner to your created owner, you can aswell in the ```Owner``` field

<img src="https://github.com/michael-j-rubenstein/learning-software-engineering.github.io/blob/main/Topics/Tech_Stacks/Images/postgres_pgadmin_database_setup.png" width=500 />

### Using Postgresql Shell
1. Open up the Mac Terminal
2. Run ```sudo -u postgres psql``` to open up the Postgresql Shell using the default postgres user and insert password
3. Now to create a new database write ```CREATE DATABASE database_name;```

Note that this makes the owner of database_name as the default postgres user. You can specify the owner by using ```CREATE DATABASE database_name OWNER your_users_name;```

You can also find more information here: [Postgresql Database: CREATE](https://www.tutorialspoint.com/postgresql/postgresql_create_database.htm)

<a name="four" />

## Viewing tables in Postgresql
This section is about how to see tables and what they contain in your Postgres database. I will be using ```database_name``` as a placeholder for your database name, and ```wanted_table``` for the table you want to inspect.

### Using PgAdmin4
1. Launch the app and insert your passwords
2. In the tab on the left side of the screen go to ```Servers > Your wanted server (Default is PostgreSQL 15) > Databases > database_name > public > Tables```
3. Now you see a list of the existing tables.
4. To see the columns in the ```wanted_table```, find it in the list of tables, click on it to expand its options, and click the ```Columns``` option. This shows all the attribute names for the table.

### Using Postgresql Shell
1. Open up the Mac Terminal
2. Run ```sudo -u postgres psql``` to open up the Postgresql Shell using the default postgres user and insert password
3. You can use ```\list``` to see the databases you have, postgres usually comes with the default databases "postgres", "template0" and "template1"
4. Now to view the tables in ```database_name``` you need to first run ```\c database_name``` to connect to it
5. If successful your shell prompt should look like the following: ```database_name=#```
6. Now to show all tables you have, you can run ```\dt```
7. To see the columns of the table you can run ```\d wanted_table```

You can also find more information here: [How to List Databases and Tables in PostgreSQL Using psql](https://chartio.com/resources/tutorials/how-to-list-databases-and-tables-in-postgresql-using-psql/) and [PostgreSQL Describe Table](https://www.postgresqltutorial.com/postgresql-administration/postgresql-describe-table/)

<a name="five" />

## Creating tables in Postgresql
This section is about how to create tables in your Postgres database. I will be using ```database_name``` as a placeholder for your database name, and ```wanted_table``` for the table you want to want to create.

### Using PgAdmin4
1. Launch the app and insert your passwords
2 In the tab on the left side of the screen go to ```Servers > Your wanted server (Default is PostgreSQL 15) > Databases > database_name > public > Tables```
3.Right click ```Tables``` and select ```Create > Table...``` and create your table

<img src="https://github.com/michael-j-rubenstein/learning-software-engineering.github.io/blob/main/Topics/Tech_Stacks/Images/postgres_pgadmin_create_table.png" width=500 />

### Using Postgresql Shell
1. Open up the Mac Terminal
2. Run ```sudo -u postgres psql``` to open up the Postgresql Shell using the default postgres user and insert password
3. Navigate to your database using ```\c database_name```
4. Use Postgresql syntax ```CREATE TABLE wanted_table(... attributes ...);``` to create the table

You can also find more information here: [Different methods to create a table in PostgreSQL](https://www.commandprompt.com/education/different-methods-to-create-a-table-in-postgresql/)

<a name="six" />

## Deleting tables in Postgresql
This section is about how to delete a table in your Postgres database. I will be using ```database_name``` as a placeholder for your database name, and ```wanted_table``` for the table you want to want to delete.

### Using PgAdmin4
1. Launch the app and insert your passwords
2 In the tab on the left side of the screen go to ```Servers > Your wanted server (Default is PostgreSQL 15) > Databases > database_name > public > Tables```
3 Find and right click ```wanted_table``` and click ```Delete/Drop``` and you're done!

<img src="https://github.com/michael-j-rubenstein/learning-software-engineering.github.io/blob/main/Topics/Tech_Stacks/Images/postgres_pgadmin_drop_table.png" width=500/>

### Using Postgresql Shell
1. Open up the Mac Terminal
2. Run ```sudo -u postgres psql``` to open up the Postgresql Shell using the default postgres user and insert password
3. Navigate to your database using ```\c database_name```
4. Use Postgresql syntax ``` DROP TABLE wanted_table;``` to delete the table

You can also find more information here: [How to drop a table in PostgreSQL]([https://www.commandprompt.com/education/different-methods-to-create-a-table-in-postgresql/](https://www.commandprompt.com/education/how-to-drop-a-table-in-postgresql/#:~:text=While%20in%20the%20case%20of,selected%20table%20from%20the%20database.))