PostgreSQL DDL Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 31: Line 31:
  createdb <''dbname''>
  createdb <''dbname''>


  ovidiu$ createdb -h localhost -U admin playground
  createdb -h localhost -U admin playground


or  
or  

Revision as of 19:01, 18 October 2018

Internal

Connect as a Specific User to a Database

psql <username> <dbname>
psql -h localhost -U admin

Database Cluster

initdb

https://www.postgresql.org/docs/9.5/static/app-initdb.html

initdb will create a new database cluster.

Database

List Databases

psql -l

or

SELECT datname FROM pg_database;

Create a Database

createdb <dbname>
createdb -h localhost -U admin playground

or

CREATE DATABASE <dbname>;

Connect to a Database

psql <database-name>

Drop a Database

DROP DATABASE

Users

Postgres Users

List Users

psql
\du

or

SELECT usename FROM pg_user;

Create User

createuser name

or

CREATE USER name;

Tablespace

List Tablespaces

psql
\db

Tables

List Tables

psql
\dt

Create a Table

CREATE TABLE poc_library_component
(
  "id" integer NOT NULL,
  "name" text,
  "desc" text,
   CONSTRAINT poc_library_component_pk PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE poc_library_component OWNER TO is3_as;

Note: apparently, Postgres does not like uppercases in column names.