14 Şubat 2019 Perşembe

pg_restore komutu

Giriş
pg_dump komutu ile alına yedekleri geri yüklemek içindir. Dump dosyası formatının text olmaması gerekir. Yoksa şu hatayı verir
$ pg_restore -U mypostgresqldumpplanet 
  -d myjhipsterplanet \
  -c \
  /var/lib/postgresql/data/2023-04-01-plaintext.sql 
pg_restore: error: input file appears to be a text format dump. Please use psql.
-c seçeneği
Restore işleminden önce veri tabanını temizler. 
Örnek - file restore
Şöyle yaparız
$ pg_restore \
  -U mypostgresqldumpplanet \
  -d myjhipsterplanet \
  -c \
  /var/lib/postgresql/data/2023-04-01-custom.dum
Örnek - directory restore
Şöyle yaparız
$ pg_restore 
  -U mypostgresqldumpplanet 
  -d myjhipsterplanet 
  -c
  /var/lib/postgresql/data/2023-04-01-directory/
Örnek - tar restore
Şöyle yaparız
$ pg_restore 
  -U mypostgresqldumpplanet 
  -d myjhipsterplanet 
  -c 
  /var/lib/postgresql/data/2023-04-01-tar.tar
-d seçeneği
Örnek
Şöyle yaparız. Burada parametreler şöyle
-U admin kullanıcı ismi 
-d veri tabanı ismi. postgres veri tabanı her zaman vardır
-C ile CREATE belirtiliyor
Restore işlemi başarısız çünkü myjhipsterplanet isimli role yok
$ pg_restore \
  -U mypostgresqldumpplanet \
  -d postgres \
  -C  \ 
  /var/lib/postgresql/data/2023-04-01-original.dump

pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 3382; 1262 16384 DATABASE myjhipsterplanet myjhipsterplanet
pg_restore: error: could not execute query: ERROR:  role "myjhipsterplanet" does not exist
Command was: ALTER DATABASE myjhipsterplanet OWNER TO myjhipsterplanet;
...
-l seçeneği
Sanırım dump dosyasının içini gösteriyor.
Örnek
Şöyle yaparız.
pg_restore -l <custom_dump_file>
Komutu başlatınca çıktı olarak şunu alırız
;
; Archive created at 2019-02-13 22:59:59 UTC
;     dbname: <database_name>
;     TOC Entries: 2615
;     Compression: -1
;     Dump Version: 1.13-0
;     Format: CUSTOM
;     Integer: 4 bytes
;     Offset: 8 bytes
;     Dumped from database version: 10.6 (Ubuntu 10.6-1.pgdg16.04+1)
;     Dumped by pg_dump version: 11.1 (Ubuntu 11.1-3.pgdg16.04+1)
;
Veri Tabanı Restore İçin
Örnek
Şöyle yaparız
[root@blt ~]# su - postgres
[postgres@blt ~]$ pg_dump -Fc TestDB> TestDB.dump
...
[postgres@blt ~]$ pg_restore -C -d postgres TestDB.dump
Tablo Restore İçin
Örnek
Şöyle yaparız
[root@blt ~]# su - postgres
[postgres@blt ~]$ pg_dump -Fc — data-only -W -dpostgres -tt1 > t1.dump
...
[postgres@blt ~]$ pg_restore — data-only -W -dpostgres -tt1new t1.dump

3 Şubat 2019 Pazar

GRANT

Giriş
Grant kelimesinden sonra ALL, INSERT, SELECT, UPDATE, DELETE, CONNECT kelimeleri gelir daha sonra ON TABLE X , ON ALL TABLES, ON DATABASE, ON ALL SEQUENCES kelimeleri gelir.

CONNECT Hakkı
Şöyle yaparız.
GRANT CONNECT ON DATABASE my_db TO my_user;
USAGE Hakkı
Şöyle yaparız.
GRANT USAGE ON SCHEMA public TO my_user;
ALL PRIVILEGES Hakkı
Örnek
Şöyle yaparız. springbootjpa veri tabanındaki tüm hakları jpatutorial kullanıcısına verir.
GRANT ALL PRIVILEGES ON DATABASE springbootjpa TO jpatutorial;
Örnek
Şöyle yaparız.
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO my_user;
Örnek
Şöyle yaparız.
GRANT ALL ON TABLE public.user_info TO "postgre-core-owneer";
ALL TABLES ve ALL SEQUENCES genellikle birlikte verilir. 
Örnek
Şöyle yaparız.
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA my_schema TO MY_GROUP;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA my_schema TO MY_GROUP;
Örnek
Şöyle yaparız. Burada ALL TABLE kullanılıyor ama ALL PRIVILEGES yerine SELECT... vs. şeklinde haklar ayrı ayrı belirtiliyor
CREATE ROLE web_anon NOLOGIN;
GRANT USAGE ON SCHEMA public TO web_anon;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO web_anon;
ALL SEQUENCES Hakkı
Şöyle yaparız.
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO my_user;


29 Ocak 2019 Salı

Sütun Tipleri - VARCHAR - Variable Length Standart Sütun Tipi

Giriş
Hibernate ile DDL üretiyorsak değişken tipimiz String ise varchar(255) olarak sütun yaratılır.

CHARACTER VARYING
Açıklaması şöyle. Yani VARCHAR ve CHARACTER VARYING aynı şey
In PostgreSQL, VARCHAR is an alias for CHARACTER VARYING. This means that you can use either CHARACTER VARYING or VARCHAR to define a column that stores variable-length character strings. However, CHARACTER VARYING is the preferred term in PostgreSQL.
VARCHAR Tipe LENGTH Verirsek
Açıklaması şöyle. Yani en fazla 10 milyon küsur bir sayı verilebilir
The limit for varchar when you specify a length limit (type modifier) is 10485760
Görmek için şöyle yaparız. Burada LENGTH  olarak 10 milyar veriliyor ve hata alıyoruz
SELECT 'x'::varchar(1000000000);
ERROR:  length for type varchar cannot exceed 10485760
VARCHAR Tipe LENGTH Vermezsek
Bir yazıda şöyle bir cümle geçiyordu. Diğer veri tabanlarında LENGTH vermezsek varsayılan değer 1.
In SQL, if you declare a string in any of the four formats, CHAR, NCHAR, VARCHAR, or NVARCHAR, without specifying its length, the string is given the length of one character. If you coerce a string using CAST or CONVERT and make the same mistake, it gets a length of thirty characters. Why one and thirty? Historical reasons only, but other RDBMSs react in similar ways.
Ancak PostgreSQL açısından durum farklı. Açıklaması şöyle. Yani length vermezsek, TEXT gibi davranır.
If you do not specify the n integer for the varchar data type, it behaves like the text data type. The performance of the varchar (without n) and text are the same.
CHAR vs VARCHAR 
Başka veri tabanlarında CHAR ve VARCHAR arasında performans farkı olabiliyor. Örneğin SQL Server için açıklama şöyle
Because of the fixed field lengths, data is pulled straight from the column without doing any data manipulation and index lookups against varchar are slower than that of char fields. CHAR is better than VARCHAR performance wise, however, it takes unnecessary memory space when the data does not have a fixed-length. So in cases where disk size is not an issue, it is recommended to use CHAR.
Ancak PostgreSQL için performans farkı yok. Açıklaması şöyle
CHAR and VARCHAR are implemented exactly the same in Postgres (and Oracle). There is no difference in speed when using those data types.

27 Ocak 2019 Pazar

Sütun Tipleri - Text - Variable Length Standart Olmayan Sütun Tipi

Giriş
PostgreSQL'de metin saklamak için şu tipler kullanılabilir.
- char(n)
- varchar(n)
- varchar
- text

- Bu yazıyla ilgili olarak Sütun Tipleri - varchar yazısına da bakabilirsiniz.
- text tipi standart SQL tipi değildir.

Altta Kullanılan Tip
Açıklaması şöyle
Regardless of whether we choose char, varchar or text , the underlying structure PostgreSQL uses is varlena
varlena için açıklama şöyle
Note: There is no performance difference among these three types, apart from increased storage space when using the blank-padded type and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) it has performance advantages in some other database systems, PostgreSQL has no such advantage.  In fact, character(n) it is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.

1. Fixed Length Alanlar
char(n) ve varchar(n) belirtilen uzunluğu geçemezler yani fixed length kabul edilirler. char(n) belirtilen uzunluktan daha kısa değerleri blank karakter ile doldurur yani padler.  Fixed Length Alanlar mümkünse tercih edilmemeli. Açıklaması şöyle
A common mistake is to choose a restrictive data type that doesn’t scale in the future. For example: A column containing some string can be of 3 types in PostgreSQL - varchar(n), character(n), text. When choosing the former two, you are restricted to the number of characters you can use. Choosing text type for data is usually harmless and is scalable to accommodate more characters in the future. And if need be, you can add check constraints to restrict data input.
2. Variable Length Alanlar
varchar ve text ise bir uzunluğa sahip değildirler yani variable length kabul edilirler. Açıklaması şöyle.
If you do not specify the n integer for the varchar data type, it behaves like the text data type. The performance of the varchar (without n) and text are the same.
text'in tercih edilmesinin sebebi isminin daha ayırd edici olması. Açıklaması şöyle.
text – for me a winner – over (n) data types because it lacks their problems, and over varchar – because it has distinct name

3. En Büyük Text Alanı
Açıklaması şöyle.
In any case, the longest possible character string that can be stored is about 1 GB.
Örnek
Şöyle yaparız.
CREATE TABLE category
(
  name text NOT NULL,
  ...
)
Örnek
Şöyle yaparız.
create table mytable (
    id INTEGER PRIMARY KEY,
    data TEXT
);
INSERT INTO mytable VALUES
    (0, 'a'),
    (1, 'b'),
    (2, 'c'),
    (3, 'd'),
    (4, 'e'),
    (5, 'f'),
    (6, 'g'),
    (7, 'h'),
    (8, 'i'),
    (9, 'j');