Skip to content

Instantly share code, notes, and snippets.

@richxcame
Created February 5, 2022 23:47
Show Gist options
  • Save richxcame/5e938c807d858b79cfe7c843d02f7ec5 to your computer and use it in GitHub Desktop.
Save richxcame/5e938c807d858b79cfe7c843d02f7ec5 to your computer and use it in GitHub Desktop.
Postgres cheat sheet

Postgres

Installation for OSX with Homebrew

brew install postgresql

Start postgres

brew services start postgresql

Default (test) database

psql postgres

Some api ref.

  • \conninfo | Connection info
  • \q | Exit psql connection
  • \c | Connect to a new database
  • \dt | List all tables
  • \du | List all roles
  • \list | List databases

Creating a role in Postgres

CREATE ROLE richxcame WITH LOGIN PASSWORD 'your_password';

Give access to create database to richxcame

ALTER ROLE richxcame CREATEDB;

Login as richxcame

psql -d postgres -U richxcame

CREATE DATABASE m111;

CREATE DATABASE m111;

Create table users

CREATE TABLE users (
  ID SERIAL PRIMARY KEY,
  name VARCHAR(30),
  email VARCHAR(30)
);

Insert some mock data

INSERT INTO users (name, email)
  VALUES ('Baygeldi', '[email protected]'), ('John', '[email protected]');

Show all users

SELECT * FROM users;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment