Postgres: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 18: Line 18:
=Useful commands=
=Useful commands=
<syntaxhighlight "psql">
<syntaxhighlight "psql">
# Show users
/* Show users */
\du
\du


# Show databases
/* Show databases */
\l
\l


# Show tables
/* Show tables */
\dt
\dt


# Create database
/* Create database */
CREATE DATABASE testdb;
CREATE DATABASE testdb;


# Use database
/* Use database */
\c testdb
\c testdb


# Create Table
/* Create Table */
CREATE TABLE employees (  
CREATE TABLE employees (  
     id SERIAL PRIMARY KEY,  
     id SERIAL PRIMARY KEY,  
Line 41: Line 41:
);  
);  


# Describe the Table
/* Describe the Table */
\d test_table
\d test_table


# Create Index
/* Create Index */
CREATE UNIQUE INDEX name_idx ON employees (name);
CREATE UNIQUE INDEX name_idx ON employees (name);


# Command History
/* Command History */
\s
\s



Revision as of 07:24, 12 June 2021

Introduction

This is a page for keeping track of using postgres

Install

Nice and easy lemon squeezy

sudo apt install postgresql postgresql-contrib

Getting to CLI

Login and envoke the CLI

sudo -i -u postgres
psql

Quiting..

\q

Useful commands

/* Show users */
\du

/* Show databases */
\l

/* Show tables */
\dt

/* Create database */
CREATE DATABASE testdb;

/* Use database */
\c testdb

/* Create Table */
CREATE TABLE employees ( 
    id SERIAL PRIMARY KEY, 
    "Name" VARCHAR (50), 
    birthdate DATE CHECK (birthdate > '1900-01-01'), 
    salary numeric CHECK(salary > 0) 
); 

/* Describe the Table */
\d test_table

/* Create Index */
CREATE UNIQUE INDEX name_idx ON employees (name);

/* Command History */
\s

Create Super User