Posts

PostgreSQL İle Replication Slot

Merhaba, PostgreSQL 9.4 ile Logical Decoding kavramı gelmesiyle Replication Slot yapısının temeli atıldı diyebiliriz. Çünkü Logical Replication, Logical Decoding yapsının üzerinde geliştirilmiştir. Sonrasında ise PostgreSQL 9.6 ile Logical Replication ve Physical Replication kavramlarıyla tanıştık. Öncesinde Replication Slot kavramından, daha sonra Logical ve Physical Replication kavramlarından bahsedeceğim. Tabii öncesinde WAL kavramıyla ilgili küçük bir açıklama yapmak isterim. Çünkü Replicatiın Slot aslında kabaca WAL dosyalarının bir yerden başka bir yere kopyalanması olarak düşünebilirsiniz.  WAL(Write-Ahead-Log) PostgreSQL de transactional loglar 16MB lik dosyalarda saklanmaktadır. Bu dosyalar WAL (Write-Ahead-Log) dosyalarıdır. Data file'lar üzerindeki değişiklikler commit edildikten sonra ancak loglanır. Log record'lar okunduktan sonra değişiklikler kalıcı olarak diske yazılır. Aslında bunların yapılmasının tek bir amacı var o da bir sorun olması durumunda WAL d

PgCluu Kurulumu

Image
Merhaba, Pgcluu, PostgreSQL cluster kullanımı istatisikleri ve veritabanına dair önemli precess'lerin, backgroung, checkpoint gibi, istatistiksel bilgilerinin toplanmasını ve bu bilgilerin grafiksel olarak son kullanıcıya aktarılmasını sağlayan açık kaynak kodlu bir araçtır. Pgcluu'nun websitesine buradan ulaşabilirsiniz. PgCluu kullanmak için ihtiyacımız olan Perl , Postgres client ve opsiyonel olarak da sar ile rapor alabilmek için sysstat paketinin kurulumu gerekir. Kurulum pgCluu kurulumunu şu şekillerde yapabilirsiniz: Red Hat Enterprise Linux / CentOS / Fedora / Oracle Enterprise Linux kullanıcıları: https://yum.postgresql.org adresindeki PostgreSQL YUM deposunu kurmak ve Fedora’da dnf, diğerlerinde de yum kullanarak paketi kurmak:   RHEL/CentOS/Oracle Linux:  yum install pgcluu   Fedora 27+:  dnf install pgcluu SLES 12: https://zypp.postgresql.org adresindeki PostgreSQL Zypper deposunu kurmak ve zypper ile paketi kurmak   zypper install pgclu

PostgreSQL'de Parolası Encrypt Edilmemiş Kullanıcılar için Authentication Düzenlemesi

Merhaba, PostgreSQL'de authentication ayarlarını pg_hba.conf dosyası içinde yapıyoruz. Pg_hba.conf dosyasına ait detaylı bilgiye buradan ulaşabilirsiniz. Pg_hba.conf içinde authentication method olarak password kullanılmışsa bunun anlamı kullanıcı parolası network üzerinden plaintext olarak aktarıldığıdır. Eğer böyle bir durumda authentication method değiştirmek istenirse izlenecek adım; Belirli bir kullanıcı için authentication method password kullanılmış ise o kullanıcının parolasının encrypt olup olmadığından pg_authid tablosuna bakılarak emin olunmalı. Eğer kullanıcı parolası encrypt edilmemişse(pg_authid tablosunda rolpassword alanına bakılmalı) o kullanıcı için ALTER ROLE .. WITH ENCRYPTED PASSWORD 'kullanıcı_parolası'; denilmeli. Kullanıcı parolası encrypted olarak saklandığına göre pg_hba.conf dosyasında password yerine 9.6 ve öncesi için en azından md5 yazılmalı. 10 ve sonrası için md5 veya scram-sha-256 kullanılabilir. (scram-sha-256, md5'ten daha g

PostgreSQL Grant Privileges To a Role

Hi all, If you have lots of user and table/schema on your database you can not give permission per table but you can follow this steps. It's gonna be manual process but sometimes you can not give whole privileges on entire schema in your databases to users/roles. So, in that case you should generate your scripts. GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO your_role; GRANT USAGE ON SCHEMA schema_name TO your_role; GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA schema_name TO your_role; If you don't want to give all privileges and make specific for users also you can generate a sql script for whole schema for user or your all users like; Generate GRANT script for granting privileges whole schemas for a role; SELECT DISTINCT 'GRANT SELECT ON ALL TABLES IN SCHEMA '|| schemaname || 'TO user_role;' FROM pg_stat_user_tables; Generate USAGE script for whole schemas; SELECT DISTINCT 'GRANT USAGE ON SCHEMA '|| schemaname || 'TO user_role;

PostgreSQL 10 Logical Replication

Merhaba, PostgreSQL 10 ile gelen yeniliklerden biri olan Logical Replication'dan bahsetmek isterim. Logical Replication (LR), veri objelerini ve üzerlerindeki değişiklikleri, replikasyon kimliklerine göre (primary key gibi) replike eder. Yani aslında publisher veritabanından datanın snapshot’ının kopyalanmasıdır. Tüm değişiklikler tamamlandığında publisher üzerindeki değişiklikler subscriber’a real time olarak yollanır. LR ile sadece normal tablolar replike edilebilir. Materialized view, view, partition’un root tablosu yada foreign tabloları replike edilemediği göz önünde tutulmalıdır. Ayrıca şema bazlı veri aktarımı yapılamamakta. Farklı PostgreSQL versiyonları arasında LR yapılabilir. Birden fazla veritabanı üzerinden tek bir veritabanına LR yapılabilir. Farklı kullanıcı gruplarına replike olmuş datalar için yetki verilebilir. Veri objesi üzerinde yapılan her tekil değişiklik karşı tarafa aktarılır. Tek bir tablonun replikasyonu birden farklı cluster’lar üzerinde olabilir. LR i

Matching PostgreSQL Client and Server Versions

Hi, When PostgreSQL server and cluster versions don't match, PostgreSQL doesn't let you to run some functions, therefore you can not use efficiently and can not be advanced on it. To solve this problem you need to upgrade version or follow bellow steps. You can try all of steps for your development or test environments. I installed PostgreSQL server and client with different versions to show what is gonna happen If I try to dump. For sure, I can not get pg_dump! -bash-4.2$ pg_dump -s testdb > testdb_dump.sql pg_dump: server version: 9.6.3; pg_dump version: 9.2.18 pg_dump: aborting because of server version mismatch -bash-4.2$ pg_ctl stop -m fast waiting for server to shut down.... done server stopped -bash-4.2$ mv /var/lib/pgsql /tmp/pgsql_cluster_old mv: cannot move ‘/var/lib/pgsql’ to ‘/tmp/pgsql_cluster_old’: Permission denied To move pgsql file, need to be root at this moment. -bash-4.2$ logout [root@gunce-test tmp]# mv /var/lib/pgsql /tmp/pgsql_clu

PostgreSQL Data Masking Tricks

Hi, SQL Server announced Dynamic Data Masking feature not long ago and I'm Postgresql DBA! PostgreSQL does not have like that feature yet. So, what if we need data masking on our database's tables? If you need make strict your database tables and hide your column but not that all follow bellow function! This function shows only the first and last two characters. If you looking for some solution for your table's column and if it is the character you can create bellow function in your database and enjoy it! CREATE OR REPLACE FUNCTION dba.pg_datamask_column(col character varying)   RETURNS character varying AS $BODY$ begin  if (select length(col))<=2 then return col; elsif (select length(col))=3 or  (select length(col))=4  then return  (select replace(col,right(col,2),'xx')); else return (select  replace(substring(col from 1 for length(col)-2), right(substring(col from 1 for length(col)-2),length(col)-4),'xxxx')||right(col,2)); end if;

How To Partitioning on PostgreSQL 9.6

Hello, I'm gonna share my experience on partitioning and show how affect partition on scripts. I'm gonna show you how to partitioning on existing table. Let's begin to following steps; Create new table. It's gonna be partitioned table. Create partition tables. Grant privileges to partition tables and partitioned table. Generate function and trigger on partitioned table. Insert data from main table to partitioned table. Create indexes if you had on main table on partition tables. Check partition tables everything is fine. Rename main table. Rename partitioned table name like main table Drop old main table. Not that: if you have some indexes or constraint on your main table, not to increase inserting time you should create your new main table and other partition tables without constraints and indexes. After insert whole data to partition tables you can add indexes and constraints. It will be help you to executing insert statement more quickly.  Bef