Etiket: postgresql

  • PostgreSQL 10’u 12’ye güncelleme

    postgresql

    Notebook’umda kullandığım PostgreSQL 10 sürümü veritabanıdağıtımını sonunda 12 sürümüne çekme kararı verdim. Daha önce kullandığım işletim sistemi emrivaki yapıp 12 sürümünü yükledikten sonra güncellemeyi de kendin yap diyince, acil işim olduğundan kurulan 12 sürümünü pruge edip 10 sürümünden devam etmiştim. Ama artık Corona sağolsun evde tıkılı kaldığım şu zamanda sallamadığım güncelleme işine girişebilirim.

    Öncelikle purge ile kaldırdığım paketi yeniden kurarak işe başladım:

    # apt install postgresql-12

    Daha sonra postgres.conf ve pg_hba.conf ayarlarını 10’dan 12’ye aktarıyoruz. Bunun için iki sürümün dosyalarındaki farklara aşağıdaki gibi bakabilirsiniz:

    # diff /etc/postgresql/10/main/postgresql.conf /etc/postgresql/12/main/postgresql.conf
    # diff /etc/postgresql/10/main/pg_hba.conf /etc/postgresql/12/main/pg_hba.conf

    Ayarları aktarma işleminden sonra çalışan postgresql servisini durdurun

    # sudo systemctl stop postgresql.service

    Servisi durdurduktan sonra yükseltme işlemini “postgres” kullanıcısı ile yapmaya başlayacağız. Bunun için “postgres” kullanıcısına geçin:

    # sudo su postgress

    Yükseltme işlemine geçmeden önce son olarak cluster’ları kontrol edelim:

    # /usr/lib/postgresql/12/bin/pg_upgrade \
    --old-datadir=/var/lib/postgresql/10/main \
    --new-datadir=/var/lib/postgresql/12/main \
    --old-bindir=/usr/lib/postgresql/10/bin \
    --new-bindir=/usr/lib/postgresql/12/bin \
    --old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \
    --new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' \
    --check

    Yukarıdaki komutun sonundaki “–check” anahtarı size yapılacak işlemi herhangi bir değişiklik yapmadan çalıştırıp (dry run) herhangi bir sorun olup olmayacağını gösterecektir. Eğer bir sorun görünmezse aynı komutu “–check” anahtarını silip tekrar çalıştıracağız:

    # /usr/lib/postgresql/12/bin/pg_upgrade \
    --old-datadir=/var/lib/postgresql/10/main \
    --new-datadir=/var/lib/postgresql/12/main \
    --old-bindir=/usr/lib/postgresql/10/bin \
    --new-bindir=/usr/lib/postgresql/12/bin \
    --old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \
    --new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf'

    Bu işlemin süresi, veri tabnaının barındığı medyanın hızı, veritabanı sayısı ve boyutuna göre değişiklik gösterecektir. HDD üzerindeki ~90GB boyutundaki veritabanını aktardığımda süre tutmadım ama en az 2 saat sürdüğünü söyleyebilirim. Yükseltme işlemi bittikten sonra “postgres” kullnıcısını terk edip yeni PostgreSQL sunucu ayarlarındaki portu olması gerekn port haline getiriyoruz. Tabi eski sunucunun port ayarını da değiştirmemiz gerekiyor:

    # sudo nano /etc/postgresql/12/main/postgresql.conf

    "port = 5433" satırını "port = 5432" olarak değiştirin.

    # sudo nano /etc/postgresql/10/main/postgresql.conf

    "port = 5432" satırını "port = 5433" olarak değiştirin.

    Ve PostgreSQL servisini yenidne başlatın:

    # sudo systemctl start postgresql.service

    Şu andan itibaren sürüm 10, 5433 portundan, sürüm 12 ise standart 5432 portundan yayın yapmaya başladı. unutmayın halen 10 sürümüne geri dönebilir durumdasınız. Sadece port numaralarını değiştirerek eskiv eri tabnına erişebilirsiniz. Ya da mevcut port üzerinden bir client ile erişilebilir. Öntanımlı portta çalışan veritabanı sunucu sürümünü öğrenmek için aşağdaki komutu girebilirsiniz:

    # sudo su postgres
    # psql -c "SELECT version();"

    Yeni veritabanı için bir takım optimizasyonlar yapmak maksadıyla aşağıdaki komutu çalıştıracağız.

    # ./analyze_new_cluster.sh
    # exit

    Tebrikler. Güncellenmiş PostgreSQL ve yükseltilmiş veritabnınız kullanıma hazır.

    Bu adımdan sonra yapılacaklar eski veritabanının ve sunucusunun sistemden kaldırılması adımlarıdır. Eğer birşeylerden emin değilseniz bu adımlara geçiş yapmayın!

    # apt list --installed | grep postgresql
    # sudo apt-get remove postgresql-10 postgresql-server-dev-10
    # sudo rm -rf /etc/postgresql/10/
    # sudo su postgres
    # ./delete_old_cluster.sh
  • Preparing virtual environment for running with apache mod_wsgi & postgresql

    For completing postgresql requirements of virtual environment, install “libpq-dev” and “python-dev” packages, outside of virtual environment.

    $ deactivate (if you are in virtual environment)
    $ sudo apt-get install libpq-dev python-dev

    Re-activate virtual environment and install psycopg2 from pip repo.

    $ pip install psycopg2

    By the way, in the name of  conformist approach install ipython in virtual environment.

    $ pip install ipython

    PostgreSQL engine and python connection libraries are ready.
    Now, lets prepare wsgi file for current project. (I assumed apache and mod_wsgi installed and configured) I’m going to use /var/www/project path for example for environment root in wsgi file.

    ENV_DIR = [‘/var/www/project’]

    import site, os, sys

    base = os.path.dirname(__file__)
    sys.path.insert(0, base)
    sys.path.append(os.path.dirname(base))

    # Remember original path.
    prev_sys_path = list(sys.path)

    # Add each new site-packages directory.

    for directory in ENV_DIR:

    site.addsitedir(directory)

    # For taking new directories to the front reorder path.

    new_sys_path = []
    for item in list(sys.path):

    if item not in prev_sys_path:

    new_sys_path.append(item)
    sys.path.remove(item)

    sys.path[:0] = new_sys_path
    os.environ[‘PYTHON_EGG_CACHE’] = ‘/tmp/project_eggs’
    sys.stdout = sys.stderr
    os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘%s.settings’ % os.path.basename(base)
    from django.core.handlers.wsgi import WSGIHandler
    application = WSGIHandler()

    Place the wsgi file above into root of the django project which created inside of the virtual environment directory (eg. /var/www/project/django/)

    So wsgi file is ready, and we can configure apache to use this wsgi file:

    <VirtualHost *:80>

    WSGIDaemonProcess project_name user=username group=usergroup processes=3 threads=15 inactivity-timeout=120 maximum-requests=50 python-path=/var/www/project/lib/pyton2.7/site-packages
    WSGIProcessGroup project_name
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptAlias / /var/www/project/django/wsgi
    ServerName project_name.com
    ServerAdmin admin@project_name.com
    CustomLog /var/www/project/django/log/access_log combined
    ErrorLog /var/www/project/django/log/error_log
    Alias /static “/var/www/project/django/static”

    </VirtualHost>

    Save this file under /etc/apace/sites-available as “project.conf”

    Run “$ a2ensite project.conf” command and restart (or reload) the apache service.

    Now you can see the running django installation at “http://www.project_name.com”

  • squeeze/sid'e PostgeSQL entegreli eJabberD kurulumu

    apt-get install ejabberd postgresql

    mkdir ejabberd-modules
    :~$ svn checkout http://svn.process-one.net/ejabberd-modules ejabberd-modules
    :~$ cd ejabberd-modules/pgsql/trunk
    :~/ejabberd-modules/pgsql/trunk$ ./build.sh
    :~/ejabberd-modules/pgsql/trunk$ cd ebin
    :~/ejabberd-modules/pgsql/trunk/ebin$ sudo cp * /usr/lib/ejabberd/ebin/
    :~/ejabberd-modules/pgsql/trunk/ebin$ cd ~/
    :~$ wget http://svn.process-one.net/ejabberd/tags/ejabberd-2.1.2/src/odbc/pg.sql

    :~$ sudo su postgres -c psql template1
    Welcome to psql 8.2.7, the PostgreSQL interactive terminal. Type: copyright for distribution terms h for help with SQL commands ? for help with psql commands g or terminate with semicolon to execute query q to quit
    postgres=# ALTER USER postgres WITH PASSWORD ‘my_postgres_user_password_here’;
    ALTER ROLE
    postgres=# q

    :~$ sudo -u postgres createdb ejabberd
    :~$ sudo su postgres
    postgres@ubuntu:~$ psql ejabberd < pg.sql
    postgres@ubuntu:~$ exit
    :~$ sudo pico /etc/ejabberd/ejabberd.cfg

    Konfigürasyon ayarlarından aşağıdaki satırda ejabberd sunucusunun dns adresi yazılır:
    {hosts, [“my_xmpp_server_dns_here”]}.

    Dahili authentication kontrolünü sağlayan bu satırın başına %% işareti koyarak kapatıyoruz:
    %%{auth_method, internal}.

    Ardından ODBC authentication ayarını açıyoruz:
    {auth_method, odbc}.

    Bu satırda da yönetici tanımlıyoruz:
    {acl, admin, {user, “myadmin_username_here”, “my_xmpp_server_dns_here”}}.

    PostgreSQL Veritabanı erişimini ayarlamayı da unutmayalım:
    {odbc_server, {pgsql, “localhost”, “ejabberd”, “postgres”, “my_postgres_user_password_here”}}.

    Eğer gerekliyse aşağıdaki satırları da değiştirebilirsiniz:
    mod_last <-> mod_last_odbc >>PostgreSQL’de son login tarihi kaydı tutulması için.
    mod_offline <-> mod_offline_odbc >> PostgreSQL’de kullanıcı offline mesajlarını tutabilmesi için.
    mod_roster <-> mod_roster_odbc >>  PostgreSQL’de kontak listesi tutlabilmesi için.
    mod_vcard <-> mod_vcard_odbc >> PostgreSQL’de kullanıcı tanımlaması tutulması için.

    Dosyayı saklayın ve çıkın.