Postgresql Notlarım

Cem Topkaya
6 min readJun 1, 2021

--

  • Docker İçinde PostgreSQL Çalıştırmak
  • Komut satırından PostgreSQL’e nasıl bağlanılır
  • Veritabanı nasıl içe ve dışa aktarılır
  • Veritabanlarını Listelemek (\list veya \l)
  • pg_dump Komut Satırı Uygulamasıyla İhraç Etmek (export)
  • pg_dump Komut Satırı Uygulamasıyla İthal Etmek (import)
  • Kullanıcı Oluşturma (createuser)
  • Kullanıcılarını Listelemek
  • Kullanıcı Silmek (dropuser)
  • Veritabanı Oluşturma
  • Ayarlarını Görüntülemek

Docker İçinde PostgreSQL Çalıştırmak

docker run -d --rm --name pg -e POSTGRES_PASSWORD=ccc -p 5432:5432 postgres

PostgreSQL İstemci Uygulamaları*

clusterdb : cluster a PostgreSQL database
createdb : create a new PostgreSQL database
createlang : install a PostgreSQL procedural language
createuser : define a new PostgreSQL user account
dropdb : remove a PostgreSQL database
droplang : remove a PostgreSQL procedural language
dropuser : remove a PostgreSQL user account
ecpg : embedded SQL C preprocessor
pg_basebackup : take a base backup of a PostgreSQL cluster
pg_config : retrieve information about the installed version of PostgreSQL
pg_dump : extract a PostgreSQL database into a script file or other archive file
pg_dumpall : extract a PostgreSQL database cluster into a script file
pg_receivexlog : streams transaction logs from a PostgreSQL cluster
pg_restore : restore a PostgreSQL database from an archive file created by pg_dump
psql : PostgreSQL interactive terminal
reindexdb : reindex a PostgreSQL database
vacuumdb : garbage-collect and analyze a PostgreSQL database

Komut satırından PostgreSQL’e nasıl bağlanılır

psql dbname username

Parola isteminde, veritabanı kullanıcısının parolasını yazın. Doğru parolayı yazdığınızda, psql istemi görünür.

Veritabanı nasıl içe ve dışa aktarılır

Veritabanlarını Listelemek (\list veya \l)

doğrudan psql komutunun parametresi olarak çalıştırılabilir:

$ psql --command=\\list
veya
$ psql -c \\du

Veya psql ile REPL üstünden sonuçları görebilmek:

cem.topkaya@CEM-TOPKAYA-PC C:\Temp
# docker run -d --rm --name pg -e POSTGRES_PASSWORD=ccc -p 5432:5432 postgres
f2c2b99bb007c1347aa61d098d4ca18cd12597d0be9345554cb4b05cb88db274
cem.topkaya@CEM-TOPKAYA-PC C:\Temp
# docker exec -it pg bash
root@f2c2b99bb007:/# su -l postgres
postgres@f2c2b99bb007:~$
postgres@f2c2b99bb007:~$ psql
psql (13.3 (Debian 13.3-1.pgdg100+1))
Type "help" for help.
postgres=# \list
List of databases
Name | Owner | Encoding | Collate | Ctype | ..
-----------+----------+----------+------------+------------+--..
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | ..
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | ..
(3 rows)
postgres=# \du+
List of roles
Role name | Attributes ..
-----------+-----------------------------------------------------..
postgres | Superuser, Create role, Create DB, Replication, Bypa..
postgres=#
$ psql — command=\\list

pg_dump Komut Satırı Uygulamasıyla İhraç Etmek (export)

  1. Veritabanının depolandığı bilgisayarda komut satırına erişin. Örneğin, veritabanı başka bir bilgisayardaysa SSH kullanarak hesaba giriş yapın.
  2. Aşağıdaki komutu yazın ve ardından Enter tuşuna basın.
$ pg_dump -U kullanıcı_adı veritabanı_adı > isteGeliyor.pgsql
Password:
$ ls isteGeliyor.pgsql
isteGeliyor.pgsql

psql Komut Satırı Uygulamasıyla İthal Etmek (import)

Dışarı aktarılan veritabanını içeri almak istediğimizde eğer veritabanı sunucusu farklı ise ve veritabanı içinde tanımlı kullanıcı eğer yeni hedef sunucuda yoksa önce oluşturmanız gerekecek!

# test isimli veritabanını dosyadan yapı ve veri olarak dolduruyoruz$ psql test < /isteGeliyor.pgsql

Kullanıcı Oluşturma (createuser)

PostgreSQL’e postgres kullanıcısı olarak bağlanmanız gerekir.

$ su -l postgres

Etkileşimli olarak kullanıcı oluşturmak için:

$ createuser --interactive --pwprompt
  1. Eklenecek rolün adını girin: kullanıcının adını yazın.
  2. Yeni rol için parola girin: kullanıcı için bir parola yazın.
  3. Tekrar girin: parolayı yeniden yazın.
  4. Yeni rol bir süper kullanıcı mı olacak?
    Komut isteminde, süper kullanıcı erişimi vermek istiyorsanız y yazın. Aksi takdirde, n yazın.
  5. Yeni rolün veritabanları oluşturmasına izin verilecek mi?
    Kullanıcının yeni veritabanları oluşturmasına izin vermek istiyorsanız y yazın. Aksi takdirde, n yazın.
  6. Yeni rolün daha fazla yeni rol oluşturmasına izin verilecek mi?
    Kullanıcının yeni kullanıcılar oluşturmasına izin vermek istiyorsanız y yazın. Aksi takdirde, n yazın.
  7. PostgreSQL, kullanıcıyı belirttiğiniz ayarlarla oluşturur.
$ createuser cem --pwprompt --login --superuser --createrole --createdb --replication
$ createuser cnrusr --login --superuser --createdb
$ createuser cnrusr --pwprompt --superuser
postgres@f4216b28bfe7:~$ createuser cem --pwprompt --login --superuser --createrole --createdb --replication
Enter password for new role:
Enter it again:
postgres@f4216b28bfe7:~$ psql -U postgres

Ayrıntılı kullanımı:

$ createuser --help
createuser creates a new PostgreSQL role.
Usage:
createuser [OPTION]... [ROLENAME]
Options:
-c, --connection-limit=N connection limit for role (default: no limit)
-d, --createdb role can create new databases
-D, --no-createdb role cannot create databases (default)
-e, --echo show the commands being sent to the server
-g, --role=ROLE new role will be a member of this role
-i, --inherit role inherits privileges of roles it is a
member of (default)
-I, --no-inherit role does not inherit privileges
-l, --login role can login (default)
-L, --no-login role cannot login
-P, --pwprompt assign a password to new role
-r, --createrole role can create new roles
-R, --no-createrole role cannot create roles (default)
-s, --superuser role will be superuser
-S, --no-superuser role will not be superuser (default)
-V, --version output version information, then exit
--interactive prompt for missing role name and attributes rather
than using defaults
--replication role can initiate replication
--no-replication role cannot initiate replication
-?, --help show this help, then exit
Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as (not the one to create)
-w, --no-password never prompt for password
-W, --password force password prompt

Kullanıcılarını Listelemek

$ psql -U postgres
postgres=# \du+
\du

Kullanıcı Silmek (dropuser)

dropuser kullanıcı_adı

Ayrıntılı:

$ dropuser --help
dropuser removes a PostgreSQL role.
Usage:
dropuser [OPTION]... [ROLENAME]
Options:
-e, --echo show the commands being sent to the server
-i, --interactive prompt before deleting anything, and prompt for
role name if not specified
-V, --version output version information, then exit
--if-exists don't report error if user doesn't exist
-?, --help show this help, then exit
Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as (not the one to drop)
-w, --no-password never prompt for password
-W, --password force password prompt

Veritabanı Oluşturma (createdb)

$ createdb dbAdi --owner dbSahibiKullanici
$ createdb test --owner cnrusr
$ createdb --help
createdb creates a PostgreSQL database.
Usage:
createdb [OPTION]... [DBNAME] [DESCRIPTION]
Options:
-D, --tablespace=TABLESPACE default tablespace for the database
-e, --echo show the commands being sent to the server
-E, --encoding=ENCODING encoding for the database
-l, --locale=LOCALE locale settings for the database
--lc-collate=LOCALE LC_COLLATE setting for the database
--lc-ctype=LOCALE LC_CTYPE setting for the database
-O, --owner=OWNER database user to own the new database
-T, --template=TEMPLATE template database to copy
-V, --version output version information, then exit
-?, --help show this help, then exit
Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as
-w, --no-password never prompt for password
-W, --password force password prompt
--maintenance-db=DBNAME alternate maintenance database
By default, a database with the same name as the current user is created.

Veritabanı Düşürmek (dropdb)

$ dropdb veritabani_adi
$ dropdb veritabanı_adı

Faal Veritabanını Değiştirmek (\connect veya \c)

Tabloları Listelemek (\dt+)

\dt+

Ayarlarını Görüntülemek

SELECT name, setting FROM pg_settings WHERE setting LIKE '/%';

--

--

Cem Topkaya
Cem Topkaya

Written by Cem Topkaya

Evlat, kardeş, ağabey, eş, baba, müzik sever, öğrenmek ister, paylaşmaya can atar, iyi biri olmaya çalışır, hakkı geçenlerden helallik ister vs.

No responses yet