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
CREATEDATABASEphishiq_db;
Create user
CREATEUSERuserPASSWORD'pass';
Grant privilage to such user
GRANT ALL PRIVILEGES ON ALL TABLES INSCHEMA public TO user;ALTERROLE user SUPERUSER;ALTERROLE user CREATEDB; ALTERROLE user WITHLOGIN;
Create table
CREATETABLEtbl([col [type]]...)
List db\l
List users\du
List tables
\dt
Change user password
ALTER USER 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 affiliations
DROP 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 databasesAND datname ='database_name';