Skip to content
Home Sinan Eldem
Software

Installing PostgreSQL on macOS, CentOS and Ubuntu and Fixing the Turkish Character Error

Sinan Eldem

• 7 min read

Installing PostgreSQL on macOS, CentOS and Ubuntu and Fixing the Turkish Character Error

Hi everyone,

For a long time I had been meaning to gather PostgreSQL resources together and write an article, and today turned out to be the day.

As you know, PostgreSQL has more advantages in many respects than similar database systems. We decided to use PostgreSQL as the database for one of our SaaS (Software as a Service) applications.

In this article you will find the commands for installing PostgreSQL on macOS, CentOS and Ubuntu, and for creating users (roles) and databases.

We will walk through installing the latest version, PostgreSQL 11.5, on our machine.

Installing PostgreSQL

Installing PostgreSQL on macOS

Assuming you have Homebrew installed on your system (if not, you can install it by following the instructions here);

Let's make sure the Brew repositories are up to date:

brew update

Installation:

brew install postgresql

This command installs PostgreSQL.

(Optional) If an older version is installed on your machine, you can upgrade your existing databases with this command:

brew postgresql-upgrade-database

To start the service automatically when the computer boots:

brew services start postgresql

To start the service now:

brew services start postgresql

To stop it:

brew services stop postgresql

To restart it:

brew services restart postgresql

Reminder Did you notice that the commands for starting at boot and starting now are the same (brew services start postgresql)? On macOS, once you start a service it will run automatically at boot until you stop it again. The same goes for stop: a service you stop will not come back at boot.

There are other ways to run it at boot as well, but since that is not the topic of this article I won't cover them here.

With the brew services list command you can see the currently installed services and their status. This command will produce output similar to the following:

Name       Status  User Plist
beanstalkd started
dnsmasq    started
nginx      started
php        started
postgresql started
redis      started
unbound    stopped

Installing PostgreSQL on CentOS

Let's make sure the Yum repositories are up to date:

sudo yum update

Installation:

sudo yum install postgresql-server postgresql-contrib

Initializing PostgreSQL:

sudo postgresql-setup initdb

Starting the service:

sudo systemctl start postgresql

To start the service automatically when the computer boots:

sudo systemctl enable postgresql

To stop it:

sudo systemctl stop postgresql

To restart it:

sudo systemctl restart postgresql

Installing PostgreSQL on Ubuntu

Let's make sure the APT repositories are up to date:

sudo apt-get update

Installation:

sudo apt-get install postgresql postgresql-contrib

Starting the service:

sudo systemctl start postgresql

To start the service automatically when the computer boots:

sudo systemctl enable postgresql

To stop it:

sudo systemctl stop postgresql

To restart it:

sudo systemctl restart postgresql

Post-Installation Steps

After the PostgreSQL database server is installed, by default it creates a user named postgres with the postgres role, as well as a system account called postgres. To connect to the PostgreSQL server, log in to your system as the postgres user and connect to the database.

Connecting to the database

psql postgres

You will be greeted by PostgreSQL's command-line tool:

psql (11.5)
Type "help" for help.

postgres=#

I will add a user named homestead with the password secret.

CREATE ROLE homestead WITH LOGIN PASSWORD 'secret';

I will grant this homestead user the permission / role to create databases:

ALTER ROLE homestead CREATEDB;

Let me list the users of this database:

\du

This command will produce output similar to the following:

                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 homestead | Create DB                                                  | {}
 sineld    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}    

Let's exit the PostgreSQL command line:

\q

Let's log in with the homestead user we created:

psql postgres -U homestead

Let's create a database named project1 with Turkish character support:

CREATE DATABASE project1 ENCODING='UTF-8' LC_COLLATE = 'tr_TR.UTF-8' LC_CTYPE='tr_TR.UTF-8' TEMPLATE template0;

Note If this command gives you an error similar to the one below, you will need to define a UTF-8 Turkish locale on your system:

ERROR:  invalid locale name: "tr_TR.UTF-8"

Fixing the problem

For macOS:

Add the following lines to whichever of ~/.profile, ~/.bash_profile or ~/.zshrc you use:

export LC_ALL=tr_TR.UTF-8
export LANG=tr_TR.UTF-8

For CentOS and Ubuntu:

sudo locale-gen tr_TR
sudo locale-gen tr_TR.UTF-8
sudo update-locale

Then let's restart the PostgreSQL service:

For macOS:

brew services restart postgresql

For CentOS / Ubuntu:

sudo systemctl restart postgresql

Run the database creation command again.

Let's list our databases:

\l

This command will produce output similar to the following:

                                List of databases
   Name    |   Owner   | Encoding |   Collate   |    Ctype    | Access privileges
-----------+-----------+----------+-------------+-------------+-------------------
 project1     | homestead | UTF8     | tr_TR.UTF-8 | tr_TR.UTF-8 |
 postgres     | sineld    | UTF8     | C           | C           |
 template0    | sineld    | UTF8     | C           | C           | =c/sineld        +
              |           |          |             |             | sineld=CTc/sineld
 template1    | sineld    | UTF8     | C           | C           | =c/sineld        +
              |           |          |             |             | sineld=CTc/sineld
(6 rows)

Let's connect to the project1 database we created with Turkish character support:

\c project1;

This command will produce output similar to the following:

You are now connected to database "project1" as user "homestead".
project1=#

Let's test our Turkish characters:

SELECT
    harf, upper(harf)
FROM (
VALUES ('ş'), ('t'), ('u'), ('a'), ('n'), ('o'), ('ö'), ('p'), ('f'), ('g'), ('ğ'), ('h'), ('ı'), ('i'), ('j'), ('k'), ('l'), ('m'), ('ü'), ('v'), ('r'), ('s'), ('b'), ('c'), ('ç'), ('d'), ('e'), ('y'), ('z')) AS
alfabe(harf)
ORDER BY 1; 

This query is taken from the PostgreSQL mailing list.

This command will produce output similar to the following:

 harf | upper
------+-------
"a" "A"
"b" "B"
"c" "C"
"ç" "Ç"
"d" "D"
"e" "E"
"f" "F"
"g" "G"
"ğ" "Ğ"
"h" "H"
"ı" "I"
"i" "İ"
"j" "J"
"k" "K"
"l" "L"
"m" "M"
"n" "N"
"o" "O"
"ö" "Ö"
"p" "P"
"r" "R"
"s" "S"
"ş" "Ş"
"t" "T"
"u" "U"
"ü" "Ü"
"v" "V"
"y" "Y"
"z" "Z"
(29 rows)

If your macOS system's locale is not Turkish, the sort order may differ. From my research, this is a Unix-specific issue you may run into. On Linux, you won't hit this problem once you run locale-gen.

These are the PostgreSQL commands you will need most often:

\q                      : Çıkış
\c __veritabanı__       : veritabanı isimli veritabanına bağlantı yapar
\d __table__            : table isimli tablo hakkında açıklama (triggerları içerir)
\d+ __table__           : table isimli tablo hakkında daha fazla açıklamal (disk boyutu kullanımı)
\l                      : Veritabanlarını listeler
\dy                     : Olayları listeler
\df                     : Fonksiyonları listeler
\di                     : Indexleri listeler
\dn                     : Şemaları listeler
\dt *.*                 : Tüm şemaların tüm tablolarını listeler
\dT+                    : Tüm veri tipleri listeler
\dv                     : Görünümleri listeler
\df+ __function__       : Fonksiyonun SQL kodunu gösterir.
\x                      : Sorgu sonuçlarını, çirkin ASCII tablolar yerine güzelleştirerek döker
\du                     : Kullanıcıları listeler
\du __kullanıcı__       : Eğer mevcutsa kullanıcıyı listeler

We have reached the end of a fairly long article.

Please keep in mind that I am not an authority on this subject; what I share comes from someone who is still learning, and it was prepared as reference material both for myself and for other beginners like me.

If you come across any missing or incorrect information, please share it in the comments below.

Happy PostgreSQL-ing :)

Sinan Eldem

Custom software and AI solutions with Laravel

Related posts

Software
6 min read

Laravel Eloquent Relationships Quick Reference Guide

This quick reference is a practical guide that explains the most common relationship types with simple examples. Covering hasOne, hasMany, belongsTo, belongsToMany and polymorphic relationships along with eager loading and performance tips, it gives developers a fast and accurate reference.

Read more