We will be going through how to setup Postgres locally on mac. We will cover the following:
- Starting Postgres
- Using psql to interact with database in command-line
- Creating your first database.
- Manage Data (Create Table, Inserting Data, Viewing Data)
- Stopping Postgres
Let's start off with making sure we have Postgres installed locally. Open up terminal on mac and run
postgres --version. You should see the version information outputted.
If you don't see the postgres version or need to install it still. Check out the Install Postgres on Mac post. Now that we have that out of the way, let's continue.
In terminal, run the following command. It will start Postgres locally.
brew services start postgresql
Next, let's go into the Postgres server now that it's up and running. Run the following:
This will enable us to start running queries on the Postgres database. Let's now create our first database. In terminal, run:
CREATE DATABASE APP;
Once the database has been created, lets set it to the database we will use to create our first table. To do this, we want to run:
Let's create our first table, insert data, and view it.
CREATE TABLE "groups" ( id serial, "title" varchar NOT NULL
Now that the table is created, let's insert our first group.
INSERT INTO groups (title) VALUES ('Red');
Lastly, let's see the data we inserted.
SELECT * FROM 'groups' LIMIT 100;
By default your computer is using your user. If you want to create new users and roles, I recommend checking out the documentation on roles.
**Note: Stop your local Postgres server, you can run:
brew services stop postgresql
In future posts, we will be diving deeper into PostGres features, extending it, and using it to build applications. Thanks.