PostgreSQL
PostgreSQL
refs: https://www.postgresql.org/download/linux/ubuntu/, https://itsfoss.com/install-postgresql-ubuntu/
Installation
- add postgresql official repo: create
/etc/apt/sources.list.d/pgdg.listwithdeb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main - import repo signing key & update
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -sudo apt-get update
- install:
sudo apt install postgresql postgresql-contrib- creates default user
postgreswith no password, default dbpostgres - after install it shows: version(Ver), Cluster, Port, Status, Owner, Data directory, Log file
- creates default user
- GUI (optional):
sudo apt install pgadmin4 - creating users
- login with default user:
sudo su postgres psqlto enter sql mode- set postgres user password:
ALTER USER postgres WITH PASSWORD 'some_password'; CREATE USER ravinder WITH PASSWORD 'my_password';(password in single quotes)ALTER USER ravinder WITH SUPERUSER;- new user can access via pgadmin4 but not yet from command line
- login with default user:
- allow other users from command line
nano /etc/postgresql/12/main/pg_hba.conf(any editor, matching installed version)- change
local all postgres peertolocal all postgres all - restart:
sudo service postgresql restart
- login:
psql -U ravinder -d postgres(psql -U user_name -d db_name)
Basic commands
service postgresqlshows available commandsservice postgresql start|stop|restart|reload|force-reload|statussystemctl disable postgresqldisable auto start on bootsystemctl enable postgresqlenable auto start on boot
psql commands
\psqlenter psql mode\llist all databases\qquit\?help\dudatabase userspsql -U user_name -d db_nameloginDROP USER ravinder;CREATE USER ravinder WITH PASSWORD 'my_password';ALTER USER ravinder WITH SUPERUSER;pg_lsclusterssee database cluster details