Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to try out Postgres on Heroku - SQL tutorial

License

NotificationsYou must be signed in to change notification settings

kickstartcoding/postgres-getting-started

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kickstart Coding Logo

Kickstart PostgreSQL: Getting Started

Tutorial for setting up PostgreSQL using Heroku.

Who is this guide for

  • This is forbrand new SQL programmers, includingcoding classstudents who want a simple and to-the-point guide on connecting toa Heroku provisioned Postgres database.

  • This guide assumes you already have fundamental Bash and Heroku knowledge. Ifyou are new to Heroku, read ourHeroku Getting Startedguide.

  • This guidedoes not support Windows. It assumes you use eithermacOS orUbuntu GNU/Linux.

This was original created for Kickstart Coding, the affordable,inclusive, and intensive coding course teaching cutting-edge Python /Django and JavaScript / React web development in Oakland, CA.Learn more and enroll here.

What is PostgreSQL?

Databases are how computers and web applications store thousands or millions ofitems of data, and look them up quickly. SQL is the most popular programminglanguage for talking to databases.

Postgres is one of the most popular and powerful databases systems outthere, and it uses a dialect of SQL.

Heroku is a hosting service that also provides a "free-tier" version ofPostgres that they will run for us.

Key terms

  • database - How web apps store lots and lots of data. In some ways,databases resemble Excel spreadsheets, because they store grids ("tables") ofdata.

  • SQL - The programming language for talking to databases.

  • Postgres - A popular and powerful database management system ("DBMS")that uses SQL.

Database terminology

Note: See bottom of this guide for an spreadsheet analogy.

  • table - A named collection of data. Consists of series of columns. Adatabase can have multiple tables.

  • column - A named column of data. A table must have 1+ columns.

  • row - A single item of data. Found within tables, and has values (e.g.table data cells) for each column in that table. A table can have 0+ rows,such as one for each user.

(Less) Key Terms

  • Sqlite - A simple, limited version of SQL, that's great for learning andsimple applications, but does not work for web apps with multiple users

  • MySQL - Another popular database and SQL dialect (bought by Oracle).MariaDB is a truly free software spin-off of this.

  • MS SQL - Another popular database and SQL dialect (by Microsoft).

  • schema - A common and flexible term that more or less means the shape orlayout of your database. In Postgres, also it means a way for a database tofurther "group together" it's tables (more or less).

  • Heroku (the business) - a company that will run your HTTP serverapplication for you, and expose it to the web. They also offer free Postgresservers, which is what we are using from them.

  • heroku (the CLI command) - A tool that Heroku the company has developedwhich we will use for interacting with your Heroku account, includingcreating new Postgres databases.

Guide

Now that you know a little about Postgres, it's time to install the client onyour system and get practice connecting to a Heroku hosted version of it.

Installing Postgres client

Ubuntu Linux users, run the following command:

sudo apt-get install postgresql-client

macOS users can't install the client separately, so run the following commandto install everything:

brew install postgres

Provisioning a Postgres database on Heroku

First you will need to have a Heroku app already created. This can be either aproject you are working on, or just a brand new thing. If you don't have anapp, you can useheroku create to create one.

  1. Provision for that app a new Postgres Database (free "hobby" tier):
heroku addons:create heroku-postgresql:hobby-dev --app pure-crag-68
  1. Connect to your database with the command-line client:
heroku pg:psql --app pure-crag-68

NOTE: For the these last commands, you will need to change "pure-crag-68" tothe similarly random name your app got when you created it.

Connecting to your database

Connect to your database with the commandline client:

heroku pg:psql --app pure-crag-68

Practicing SQL

For the second challenge, create the tables necessary to house the followingdata. Add the data, and practice querying it.

Example code to run with Postgres:

  • Create a new table
CREATETABLEpeople (    idnumint,    first_namevarchar(255),    last_namevarchar(255));
  • Check which tables we have
\d\d people
  • Add data to a table
INSERT INTO people (idnum, first_name, last_name)VALUES (1,'Jane','Hacker');
  • Check the data in a table
SELECT*FROM people;
  • Challenge: Add data for a person with first name"Dolan" and last name"Duck", and ID number2. Ensure it is added withSELECT.

Playing with test data

Included in this repo is a SQL file that contains information on the 50 statesin the USA, including Puerto Rico and District of Columbia.

  1. Copy & paste that SQL file into your window (or be clever and redirect itinto the Postgres prompt using Bash).

  2. View the data:

SELECT*FROM states;
  1. Try usingWHERE in conjunction toSELECT to look for states withdifferent characteristics (will have to Google for more information onWHERE). (e.g. Only mid-sized states, with population between 5 and 10million).

  2. Try usingORDER BY in conjunction toSELECT to sort the output bydifferent orderings. (e.g. Sorted by population, least populated first)

  3. Try usingLIMIT to only select the top state that matches a certainordering and criteria. (e.g. only the most populous state that starts with theletterM).

Extra challenges

  • Look up remaining SQL commands. Get practice with all the different optionsforSELECT,INSERT,DELETE,UPDATE.

  • If you have time, look up online tutorials on installing and running Postgreslocally (as opposed to Heroku). This is something you may need to do at somepoint in the future, so doesn't hurt playing around with that now. It isn'ttoo hard, though sometimes can have hang-ups.

SQL Tips

  • Don't forget to end all your SQL statements with ";"! Otherwise it will thinkyou are still adding on to the same statement, and typically result in asyntax error.

  • If you use an ORM, such as Django ORM, it is actually writing and reading theSQL code for you under the hood. Even using an ORM, you will often need toknow how to use SQL for certain operations, or at least understand what ishappening.

  • All versions (dialects) of SQL are about ~90% the same, so if you learn one,you'll be able to use others.

  • If you are having a hard time understanding DBs, it might help to think ofthem as kind of like a spreadsheet software, such as Microsoft Excel. Thisis a very rough analogy, but maybe this will help:

SQL termExcel
database.xlsx file
tableworksheet
columncolumn
rowrow

About

How to try out Postgres on Heroku - SQL tutorial

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp