PostgreSQL

Installation

Arch Linux

yay -S postgresql 
su - postgres -c "initdb --locale en_US.UTF-8 -D '/var/lib/postgres/data'" 

sudo systemctl enable postgresql 
sudo systemctl start postgresql

Debian

sudo apt install postgresql

Commands

  • \l List all databases
  • \c database Connect to a database

Tasks

-- Rename database
ALTER DATABASE people RENAME TO customers;

Exporting / Importing

Into sql file

sudo -u postgres pg_dump databas > /tmp/database.sql

Into csv file

sudo -u postgres psql \c database \copy (SELECT * FROM table_name) TO '/tmp/table.csv' DELIMITER ',' CSV HEADER

From sql file

bash sudo -u postgres psql -d database -f database.sql

From one table to another

bash pg_dump -U postgres -t table_to_copy | psql -U postgres -d target_db