Difference between revisions of "PostgreSQL"
Jump to navigation
Jump to search
(7 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
− | ''' Cara akses ''' | + | ''' Cara akses PostgreSQL''' |
+ | |||
Jika kita mempunyai database dengan nama psql | Jika kita mempunyai database dengan nama psql | ||
Cara 1 | Cara 1 | ||
− | |||
sudo -i -u postgres | sudo -i -u postgres | ||
psql | psql | ||
Line 15: | Line 15: | ||
Cara 2 | Cara 2 | ||
sudo -u postgres psql | sudo -u postgres psql | ||
+ | |||
+ | Cara 3 (jika kita tidak akan menggunakan sudo - untuk user biasa) | ||
+ | Buat terlebih dahulu usernya | ||
+ | sudo -u postgres psql | ||
+ | |||
+ | postgres=# create database mydb; | ||
+ | postgres=# create user myuser with encrypted password 'mypass'; | ||
+ | postgres=# grant all privileges on database mydb to myuser; | ||
+ | |||
+ | psql -U myuser mydb | ||
+ | |||
+ | ''' Perintah PostgreSQL ''' | ||
+ | |||
+ | '''Membuat user''' | ||
+ | sudo -u postgres createuser --interactive | ||
+ | |||
+ | '''Membuat database''' | ||
+ | sudo -u postgres createdb <namadb> | ||
+ | atau | ||
+ | sudo -i -u postgres | ||
+ | psql | ||
+ | create database <namadb> | ||
+ | |||
+ | '''Cek database''' | ||
+ | \list | ||
+ | |||
+ | '''Pilih database''' | ||
+ | \connect <namadb> | ||
+ | |||
+ | '''Delete postgres user''' | ||
+ | REASSIGN OWNED BY myuser TO postgres; | ||
+ | DROP OWNED BY myuser; | ||
+ | DROP USER myuser; | ||
+ | |||
+ | ===Untuk di server=== | ||
+ | Selain setting security groups di server, setting juga di etc/postgresql/<version>/main/pg_hba.conf | ||
+ | |||
+ | host all all 127.0.0.1/32 md5 | ||
+ | host all all 10.0.1.0/24 md5 | ||
+ | |||
+ | Dan di etc/postgresql/<version>/main/postgresql.conf | ||
+ | Edit postgresql.conf and change listen_address: | ||
+ | |||
+ | listen_addresses='*' | ||
+ | |||
+ | |||
+ | |||
+ | ==Referensi== | ||
+ | * https://serverfault.com/questions/336596/opening-port-5432-on-ubuntu | ||
+ | * https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e | ||
+ | * https://phoenixnap.com/kb/delete-postgres-user |
Latest revision as of 13:34, 4 November 2022
Install PostgreSQL di Ubuntu 20.04.01
sudo apt update sudo apt install postgresql postgresql-contrib
Cara akses PostgreSQL
Jika kita mempunyai database dengan nama psql
Cara 1
sudo -i -u postgres psql
Cara 2
sudo -u postgres psql
Cara 3 (jika kita tidak akan menggunakan sudo - untuk user biasa) Buat terlebih dahulu usernya
sudo -u postgres psql
postgres=# create database mydb; postgres=# create user myuser with encrypted password 'mypass'; postgres=# grant all privileges on database mydb to myuser;
psql -U myuser mydb
Perintah PostgreSQL
Membuat user
sudo -u postgres createuser --interactive
Membuat database
sudo -u postgres createdb <namadb>
atau
sudo -i -u postgres psql create database <namadb>
Cek database
\list
Pilih database
\connect <namadb>
Delete postgres user
REASSIGN OWNED BY myuser TO postgres; DROP OWNED BY myuser; DROP USER myuser;
Untuk di server
Selain setting security groups di server, setting juga di etc/postgresql/<version>/main/pg_hba.conf
host all all 127.0.0.1/32 md5 host all all 10.0.1.0/24 md5
Dan di etc/postgresql/<version>/main/postgresql.conf Edit postgresql.conf and change listen_address:
listen_addresses='*'