Start Using PostgreSQL with Terminal on Mac
By: Shirley Li
Tags: Postgresql, Terminal, Sql, Postgres, Command Line

As a rookie, I was totally lost in the Relational Database Course during USF’s intensive Data Science Bootcamp last year. Don’t get me wrong. Our instructor is such a nice guy and just kicked out 10% of our classmates after the Bootcamp. 🙃 Anyway, in such a stressful environment, I have received much help from my classmates and really appreciate their selflessness. Thanks to them, I luckily passed the Bootcamp and now summarized some helpful steps to start PostgreSQL for the rookies like me.

Step 1: Brew Install PostgreSQL

If you haven’t installed Homebrew, you can run the following command lines on your terminal.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Once we got Homebrew, the installation of PostgreSQL becomes so easy!

After we run update brew on the terminal, we can simply use the command lines below to install PostgreSQL.

update brew

brew install postgresql

After the completion of the installation, we can turn on or turn off PostgreSQL by using the terminal commands below.

brew services start postgresql

brew services stop postgresql

Step 2: Database Configuration and Administration

Once we start PostgreSQL, now we can enter the PostgreSQL interactive terminal with the command psql .

psql postgres

If you experienced “ psql: could not connect to server ” error, you can find the solution here.

Successfully enter the PostgreSQL terminal

All we can do in the pgAdmin (an administration and development platform for PostgreSQL) should also be done in the terminal now. So I am going to introduce some widely used commands here. More options and usages can be found at the docs of PostgreSQL.

Users Administration:

Show all users: \du

Create a user(role): CREATE ROLE <username> <option>;

Change a user: ALTER ROLE <username> <option>;

Remove a user: DROP ROLE <username>;

Create a User (Role)

Alter a User and Drop it

What are in <> should be replaced with your own information.

Please make sure to add ; at the end of most command lines, to indicate the completion of your commands. (when you see postgres-# at the beginning of each line, it means that you are still editing your commands.)

Database Administration:

Show all the databases: \list

Create a database: CREATE DATABASE <database_name> OWNER <username>;

Change a database: ALTER DATABASE <database_name> <option> docs here

docs here Remove a database: DROP DATABASE <database_name>

An Example of Database Management

Connecting Database

User Login: psql postgres -U <username>;

Connect to a Database: \connect <database_name> [<username>];

An example of connecting databases

Leave the PostgreSQL terminal: \q

STEP3: Create Schemas and Tables in the Database

Once we successfully connect to the database, we can create schemas and tables in it by simple commands.

Schemas Management

Show all Schemas: \dn

Create a Schema create schema <schema_name>;

Drop a Schema drop schema <schema_name>;

Example of Manipulating Schemas

Create a Table and Load Data from CSV File;

Here’s an example from the PostgreSQL Tutorial.

Create a Table (Need to set up column names and types):

CREATE TABLE schema_name.tablename

(

col_name1 serial NOT NULL,

col_name2 character varying(50),

col_name3 character varying(50),

col_name4 date,

col_name5 character varying(255),

CONSTRAINT tablename _pkey PRIMARY KEY (col_name1)

);

Import Data (CSV file) into the table:

COPY schema_name.tablename FROM ‘C:\tmp\xxxx.csv’ CSV DELIMITER ‘,’ HEADER;

HEADER here indicates the first line of the CSV file is the header rather than data.

Import Data (TDF file) into the table:

COPY schema_name.tablename FROM ‘C:\tmp\xxxx.csv’ CSV DELIMITER AS E'\t';

Here’s the Documentary of Create Tables and Copy Tables.

Finally!!

Okay! After all these steps, now we should be able to load the data in our PostgreSQL database and practice SQL queries for study or work!

Thanks for reading!