dockerrun--namepg_docker \-p5432:5432 \-ePOSTGRES_DB=db-name \-ePOSTGRES_PASSWORD=pass \-dpostgres# Access from hostpsql-hlocalhost-p5432-ddb-name-Upostgres--password
Execute commands from current user (without user on db)
echo"\d"|sudo-upostgrespsql
Create database
CREATE DATABASE phishiq_db;
Create user
CREATEUSERuser PASSWORD 'pass';
Grant privilage to such user
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO user;ALTER ROLE user SUPERUSER;ALTER ROLE user CREATEDB; ALTER ROLE user WITH LOGIN;
Create table
CREATE TABLE tbl([col [type]]...)
List db\l
List users\du
List tables
\dt
Change user password
ALTERUSER davide WITH PASSWORD 'hu8jmn3';
List constraint for a table
\d+ tablename
Create constraint
ALTER TABLE raw_backup ADD CONSTRAINT raw_backup_pk UNIQUE (ticker, period, date);
Create index
CREATE INDEX idx_date_backup ON raw_backup(date);
Remove constraint
ALTER TABLE affiliationsDROP CONSTRAINT affiliations_organization_id_fkey;
Generating dump file
pg_dump-Upostgres--schema-onlydb_name>file.txt
Loading dump file
psql-Uusernamedbname<dbexport.pgsql
Kill postgresql session
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE -- don't kill my own connection! pid <> pg_backend_pid()-- don't kill the connections to other databases AND datname ='database_name';