Create a postgresql schema

The order in how to create a postgresql schema can be not obvious.
So here sql script as example,
So you first create :
1. A user
2. A database
3. A schema
4. Grant rights from the user to the schema.
5. Last create a table

Code:
CREATE USER john7 WITH PASSWORD 'john7';
CREATE DATABASE x7;
\c x7;
CREATE SCHEMA schema7;
GRANT USAGE, CREATE ON SCHEMA schema7 TO john7;
SET search_path TO schema7;
SET ROLE john7;
CREATE TABLE schema7.products7 (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    stock INT DEFAULT 0
);
\dt schema7.*
 
Back
Top