Signup to the waiting list!
The following instructions to install PostgreSQL are based on macOS.
For Windows and Linux, go to https://www.postgresql.org/download/ and choose your package.
Also search “how to install postgres on windows” or “how to install postgres on
your linux distribution” if you’re using other platforms.
It should not differ a lot, especially past the installation phase.
Once you are done, get back and in the command line run:
brew install postgresql
and after it finished, run:
brew services start postgresql
to start PostgreSQL as a daemon, which means it will keep running in the background, listening for connections.
Using Homebrew has the great advantage that any update can be installed by running
brew upgrade postgresql brew postgresql-upgrade-database brew services restart postgresql
postgresql is the more complex to pronounce name of PostgreSQL, but they are the same thing. It just embeds SQL in the name. What’s SQL? SQL, pronouced “sequel”, means Structured Query Language, and it’s a special language we use to interact with a relational database.
If you’re new to database, it’s a lot of new terms for you! Basically a relational database organizes the data into tables, and provides a way to insert and extract data from those tables. That’s SQL.
And we’re going to use it soon.
Right after we log in to PostgreSQL!
Go back to the command line, and type
This will give you access to the
postgres database, which is created by default, with your macOS username. Homebrew automatically created your user at installation.
Now that we are into the
psql application, we can create a new database:
CREATE DATABASE test;
Don’t forget the semicolon
;, because it’s needed by SQL otherwise the command will not run.
Now in a new line, we switch to this database using
The prompt will tell something like this:
You are now connected to database "test" as user "flaviocopes"..
Now we’re going to create a new table.
Use this syntax:
CREATE TABLE users ( id SERIAL PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL );
Now if you run this, and no error shows up, you will have the table in the system.
You can see it by running the command
which will show you the database tables:
If you did any error, you can delete the table by running the command
DROP TABLE users
To finally quit
or just type
Now that you know how things work under the hood, I will show you an easier way to work with PostgreSQL, and other databases too: the TablePlus app. It works on macOS, Windows and Linux.
Connect to the database:
test database name:
In addition to be able to inspect tables with a Graphical User Interface:
It also allows us to work with SQL queries, very easily:
More database tutorials:
- The MongoDB basics tutorial
- How MongoDB is different from a SQL database
- Information systems, data and information
- Data models
- Introduction to the ER Data Model
- The Relational Model
- Relational Algebra
- Relational Databases
- What is a Database? And a DBMS?
- Introduction to SQL
- How to install PostgreSQL on macOS
- Do you always need a database for your app?
- How to install SQLite on macOS
- Introduction to PostgreSQL
- PostgreSQL User Permissions
- How to list all users in PostgreSQL
- How to switch database using PostgreSQL
- How to list all databases using PostgreSQL
- How to list tables in the current database using PostgreSQL
- How to install MySQL on macOS
- Creating a user on MySQL
- PostgreSQL vs MySQL, a comparison
- SQLite User Permissions
- MySQL User Permissions
- SQL, creating a table
- SQL, adding data to a table
- SQL, how to use SELECT
- SQL, Handling empty cells
- SQL, Unique and Primary keys
- SQL, how to update data
- SQL, how to update a table structure
- SQL, how to delete data and tables
- SQL Joins
- SQL Views
- How to insert multiple items at once in a MongoDB collection
- How to remove all items from a MongoDB collection