Programming/PostgreSQL/Table Queries: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Create page)
 
(Update page)
Line 9: Line 9:


Alternatively:
Alternatively:
  SELECT * FROM pg_catalog.pg_tables;
  SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public';




Line 18: Line 19:
== Show Table Indexes ==
== Show Table Indexes ==
  SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '<table_name>';
  SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '<table_name>';
== Create Table ==
CREATE TABLE test_tables__col_str (
    <columns>
);
Where {{ ic |<columns>}} is replaced by the actual columns desired for the table.
For example, to create a table with columns of {{ ic |id, name, description}}, we can do:
CREATE TABLE test_tables__create__success (
    id serial PRIMARY KEY,
    name VARCHAR(100),
    description VARCHAR(100)
);
== Delete Table ==
DROP TABLE <table_name>;

Revision as of 01:01, 27 January 2023

All of the following assume you have first loaded a database.


Show Tables

\dt

Or for expanded data:

\dt+

Alternatively:

SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public';


Show Extra Relational Data About Table

\d <table_name>


Show Table Indexes

SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '<table_name>';


Create Table

CREATE TABLE test_tables__col_str (
    <columns>
);

Where <columns> is replaced by the actual columns desired for the table.

For example, to create a table with columns of id, name, description, we can do:

CREATE TABLE test_tables__create__success (
    id serial PRIMARY KEY,
    name VARCHAR(100),
    description VARCHAR(100)
);


Delete Table

DROP TABLE <table_name>;