We're going to set up a postgres database and play with it a bit to understand its structure!
A structured set of data held, usually in files, in a computer that is accessible in various ways.
A common database system for modern web development is called PostgreSQL.
PostgreSQL (the name is interchangeable with postgres) is actually a server that runs a database. Therefore, it can be started, stopped, and interacted with as you might expect - through an interface.
In this challenge, you will install the PostgreSQL database system, and interact with it to create a database.
- Install PostgreSQL.
- Create a database.
- Install the
postgresqlcommand-line package via Homebrew (use the commandbrew). - Start postgres and set it to run automatically when your computer starts. (See the instructions in the output that brew shows when it's installing postgres.)
- Check your installation by running
psqlin the terminal. Use the resources below to understand any errors that you see. - Use the
CREATE DATABASEcommand inpsqlto set up a PostgreSQL database with the same name as your computer username, e.g.timmy507. PostgreSQL will connect to this database on startup.
In this part, you will use Postgres' built-in command-line interface to interact with the database using a database-specific language, SQL. You will create and drop a table with some columns and record your changes to the database schema.
You rarely have to interact with databases from the command-line like this (why?), but play around to create and destroy a few tables so you know what's going on at the data storage level.
- Use the
psqlcommand to interact with Postgres - Create and drop tables using SQL.
- Create a new PostgreSQL database for - give it a name
- Use
psqlto connect to this new database. - Use
psqlto create a table (give it a name) with at least two columns:id, aSERIAL PRIMARY KEY, and any others - explore different data types - Now drop the table. And repeat.
- Record the database setup instructions, so you can do it again and again,
CLICK ME
- All of the commands you will need for this step are listed in the two documents listed below. You may need to check both of them.
- Don't forget that you can list databases using
\land tables using\dt. You can use these commands to check that your set up has been successful.
You will use SQL to create, read, update and delete (CRUD) data in the table you created, inside your PostgreSQL database. The vast majority of queries will be SELECT statements.
- Use SQL terms like
SELECT,FROM,WHEREand*to query a database table - Use SQL terms like
INSERT,UPDATEandDELETEto create, update and delete database entries
- Display the current state of your table
- INSERT at least 2 more records in your table, using an
INSERTstatement. - SELECT all the records using a
SELECTstatement. - DELETE only one of the records using a
DELETEstatement. - Update one of the records using an
UPDATEstatement. - How do you know if each of the statements was successful - how is it verified?
CLICK ME
- Again, use the documents linked below to look up the commands you need.
- Sanity check each step using a SELECT statement.
- PostgreSQL Command Line Cheat Sheet
- Alternative PostgreSQL Cheat Sheet
- SQL in One Page
- Relational Database: Terminology
- Configure an application to connect to a database using an ORM
- Choose one of the ORMs below and follow the setup
- Configure an application to connect to a database using an ORM
- Successfully persist data in your database from your application - only by interacting with your application using your browser.