Management of PostgreSQL on Linux and most common SQL statements - Davz33/tutorials GitHub Wiki

Note: at the time this tutorial was first drafted, the employed PostgreSQL version was the 14.0 .
In most cases, the following is still applicable to more recent releases.
SQL statements hereby apply in most cases to any SQL-supported database with minor differences.

Preparation

Upon fresh install, you'll need to log in as postgres to run the following statements. You can switch to the user postgres via: :
sudo -i -u postgres or su - postgres

by the Ctrl+D keystroke you'll go back to your prior log-in.


Access & Metadata

Shell statements

Create a new user

createuser username

Create a new database

createdb dbname

Run .sql file from command line

psql -h host -U usr -d myDB -a -f createtable.sql

psql statements

First, enter psql
sudo -u postgres psql

by the Ctrl+D keystroke you can exit psql.

List all databases

\l

List all users and their roles' attributes

\du+

switch to a database

\c dbname

SQL statements

First, enter psql
sudo -u postgres psql

Change a user's password

ALTER USER username WITH ENCRYPTED PASSWORD 'password';

Grant (all) privileges to a user

First access the desired db
\c dbname
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schemaname TO user;
the default schema being public

List all users in current database

SELECT usename AS role,  
 CASE  
     WHEN usesuper AND usecreatedb THEN  
	   CAST('superuser, create database' AS pg_catalog.text)  
     WHEN usesuper THEN 
	    CAST('superuser' AS pg_catalog.text)
     WHEN usecreatedb THEN 
	    CAST('create database' AS pg_catalog.text)
     ELSE 
	    CAST('' AS pg_catalog.text)
 END attributes
FROM pg_catalog.pg_user
ORDER BY role desc;

List all table sizes

and show them by descending size

SELECT table_schema, table_name, pg_relation_size('"'||table_schema||'"."'||table_name||'"')
FROM information_schema.tables
ORDER by 3 desc;

Data

Shell statements

Move a table to a different database

pg_dump -t table_to_copy source_db | psql target_db

dump entire database or specific tables

pg_dump -U postgres -h <hostname> -vbCO -t <table names pattern> -f <yourdumpfilepath.sql> postgres

save file-system data folder

First check whether your PGDATA var is set: echo $PGDATA.
If not, cat /etc/postgresql/<postgresql version>/main/postgresql.conf | grep data_directory.
By default, it should be /var/lib/postgresql/<postgresql version>/main.
In which case: mkdir ~/mypostgrebackup && cp -r /var/lib/postgresql/<postgresql version>/main/* ~/mypostgrebackup/

Copy table data into csv via psql

psql -U postgres -d postgres -h <host IP> -P format=unaligned -P tuples_only -P fieldsep=\, -c "SELECT * FROM <tablename>" > mysqldump.csv
 -c \
psql -U postgres -d postgres -h <host IP> -P format=unaligned -P tuples_only -P fieldsep=\, -c "COPY table FROM STDIN WITH DELIMITER ',' CSV" > mysqldump2.csv

Copy table data from dump / csv via psql

This mostly refers to the use-case where your dump resides somewhere else than the db.
In here, we assume a CREATE statement for table already ran. We assume as well, that the delimiter of choice during the dump was the semicolon and that the table headers were included.

psql -p <port> -U <username> -h <host IP> -d <dbname> -c \
"\copy <tablename> FROM '<dump file path>' WITH CSV DELIMITER ';' HEADER;"

if you want to skip some columns:
\copy (select col1,col2 from <tablename>) FROM '<dump file path>' WITH CSV DELIMITER ';' HEADER;
that assumes you have some DEFAULT replacements for columns you are skipping; another use-case is the target table not including some source table columns, altogether. Thereby, (select * from) can be used, or, if specified, the columns must all belong to the target table too.

SQL statements

First, enter psql
sudo -u postgres psql

Copy table content to csv file

COPY tablename TO '/path/to/records.csv' WITH CSV DELIMITER ';' HEADER;

Copy table content into existing table from semicolumn delimite csv file

If you haven't yet, you must init the table structure first.
Let's say that the tablename populated table resides on db sourcedb:
pg_dump sourcedb -t 'schema-name.tablename' --schema-only
Now you can run the create statement into your target db. Next step is to psql into your target db and run:

COPY (SELECT * FROM tablename) FROM '/tmp/test.csv' WITH CSV DELIMITER ';' HEADER;

note: it might well be that your FROM path refers to a machine other then the one the target db resides on (e.g., a docker container, a remote server). In this case you might want to follow this instead.

List all primary keys

SELECT conrelid::regclass AS table,  
       conname AS primary_key,
       pg_get_constraintdef(oid) AS constraint_definition
FROM   pg_constraint
WHERE  contype = 'p'
AND    connamespace = 'public'::regnamespace
ORDER  BY conrelid::regclass::text, contype DESC;

Add a primary key to a table

ALTER TABLE tablename ADD PRIMARY KEY ("column1","column2");

Create SQL table from query

CREATE TABLE top_performers
AS (SELECT *
    FROM performers
    WHERE performance > 1000);

Change tables' name

Beware of tables' name changes. If existing queries, views, user-defined functions, stored procedures, or programs reference that table, the name change will nullify those objects.

ALTER TABLE william RENAME TO william_the_II;

Delete a table

If you don't find yourself in the Database you want to operate, type and enter \q to go back to the main shell, then log into the desired db using
psql -d dbname and finally
DROP TABLE tablename;

Duplicate database

Create an exact copy of the database
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

⚠️ **GitHub.com Fallback** ⚠️