How to Start Using PostgreSQL and Migrate From MongoDB

This lesson is an introduction to the popular and widely used open-source SQL database, PostgreSQL.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two

For our SQL database, we'll choose Postgres. Postgres is a very powerful relational database and it has the highest rating for SQL compliance. Postgres is a fully open source project and is developed and maintained by a large and active community. Postgres has earned a strong reputation for its reliability and robust feature set, becoming de-relational database of choice for many developers. To install Postgres, head over to their official download page and select the operating system you're using. Next, click the "Download the installer" button and choose the latest version for your operating system. Once the download is complete, run the Postgres installer, make sure you select all the components for installation. Because I already have an existing version of Postgres installed on my computer , it will be updated to the version I just downloaded. Note, if you are installing Postgres for the first time, you'll be asked to pick a directory to store your data, create a password for a super user, and choose a port number for your Postgres server. For this, I recommend using the default 5432 port number. Once the installation completes, you should be able to find PG Admin on your computer. PG Admin is an application that can be used to create, modify, and delete your Postgres databases using either SQL or a graphical user interface. When you launch PG Admin, you'll be asked to enter a password. This is the same password you set up during the installation process. On the left navigation panel, you'll find your databases. To create a database, simply right-click on databases and select Create Database. Next, enter a name for your new database. For this example, I will enter TestDB001. Then hit Save. To create a table for your database, select schemas and right-click on tables. Then select Create Table. To create table dialog window, enter a name for your new table. For this example, I will enter Test users. Next, navigate to the Columns tab to start building a schema for your new table . Click the plus icon to add a new column. For the first column, specify ID as the name and select Text as the data type. Set the Not, No, and the primary key constraints to Yes. The Not, No constraint ensures the column cannot contain any No values. The primary key constraint ensures the column must contain unique values, which can be used to identify each row of data. For the second column, specify Name as the name and select Character Varying as the data type. And specify 100 for the length constraint. Set the Not, No constraint to Yes. The Text data type in Postgres is equivalent to string in TypeScript. The Character Varying data type is the same as Text, except we can specify a maximum number of characters. In our case, we picked 100 characters as the max. We'll explore more data types in the next few lessons as we convert our apps to use Postgres. But for now, if you want to take a look at the full list of Postgres data types , I encourage you to check out the official documentation. If we click on the Save button right now, we would have created a new table using the Graphical User Interface. However, if we navigate to the SQL tab, we can actually see the SQL statement for doing the exact same thing. If we copy the statement, click Cancel. Select Tools, Query Tools from the top navigation bar, and execute this statement, we can create a new table using SQL. Next, let's execute some CRUD operations using SQL statements. To insert data into a table, we can run the Insert statement. This is equivalent to MongoDB's Insert 1 function. To see all of the data in the table, we can run the Select statement. This is equivalent to MongoDB's Find function. To find a specific row of data, we can also run the Select statement with a Wear class. This is equivalent to MongoDB's Find function with a Query object. To update an existing row of data, we can run the Update statement. This is equivalent to MongoDB's Update 1 function. To delete an existing row of data, we can run the Delete statement. This is equivalent to MongoDB's Delete 1 function. We can modify our table using the Alter table statement with the Rename column and the Add column clauses. We can rename one of our existing columns into username and add a new column called the email. We just modified our database schema using SQL. Finally, let's test our new database schema by inserting a new user. There are a lot more SQL statements than the ones we just explored, and you can create an entire course on SQL alone. If you are interested, I encourage you to check out W3School's SQL tutorial. However, the ultimate goal for us is not to create SQL statements manually. What we want is to have our node server generate SQL statements based on user inputs from our React application. For that, an ORM is the perfect tool to use. [BLANK_AUDIO]