How to Solve Error Message pg_ctl: no database directory specified and environment variable PGDATA unset when starting PostgreSQL database server using pg_ctl

Posted on

Introduction

This article is focusing on how to solve an error message appear on starting PostgreSQL database server. Actually, this article has a connection with another article. It is a previous article with the title of ‘How to Solve Error Message pg_ctl: no operation specified when starting PostgreSQL database server using pg_ctl’. The article itself is available in this link.  It contains information about an error appear when executing ‘pg_ctl’ command to start PostgreSQL database server. The following is the complete display of the execution :

[pgsql@10 bin]$ /opt/postgresql-12.0/app/bin/pg_ctl start
pg_ctl: no database directory specified and environment variable PGDATA unset
Try "pg_ctl --help" for more information.
[pgsql@10 bin]$

Solution

As in the previous article, the solution is very simple. In order to execute the command ‘pg_ctl’ for starting PostgreSQL database server, it is not enough only for specifying the operation parameter. In order to start the PostgreSQL database server, the command ‘pg_ctl’ also need another parameter. That parameter is the parameter for specifying the location of the data directory of the PostgreSQL database server.

For complete reference, read several other articles available. First of all, it is an article with the title of ‘How to Solve Error Message no database directory specified and environment variable PGDATA unset when starting PostgreSQL Database Server in Linux’. Just access it in this link.

The second one is an article which is also suitable as a reference. It is the article with the title of ‘How to Solve Error Message postgres does not know where to find the server configuration file when Starting PostgreSQL Database Server in Linux’.  The article itself exists in this link. So, the full step of the solution exist as follows :

  1. Make sure to use the user account for managing the PostgreSQL service and data directory. For an example, as in the initialization process of the PostgreSQL database server in another in this link, it is ‘pgsql’. It is an article with the title of ‘How to Initialize PostgreSQL Database in Linux’. Execute the following command to switch account to ‘pgsql’ :

    [root@10 ~]# su - pgsql
    [pgsql@10 ~]$
    
  2. Execute the command using the following pattern :

    /app_folder_location_of_postgresql_database_server/bin/pg_ctl -D /data_folder_location_of_postgresql_database_server operation_parameter
    

    For an example :

    /opt/postgresql-12.0/app/bin/pg_ctl -D /opt/postgresql-12.0/data start
    

    And the execution of it exist as follows :

    [pgsql@10 bin]$ /opt/postgresql-12.0/app/bin/pg_ctl -D /opt/postgresql-12.0/data start
    waiting for server to start....2021-06-17 03:27:26.472 EDT [87496] LOG: starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
    2021-06-17 03:27:26.472 EDT [87496] LOG: listening on IPv6 address "::1", port 5432
    2021-06-17 03:27:26.472 EDT [87496] LOG: listening on IPv4 address "127.0.0.1", port 5432
    2021-06-17 03:27:26.477 EDT [87496] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
    2021-06-17 03:27:26.496 EDT [87497] LOG: database system was shut down at 2021-06-17 03:01:57 EDT
    2021-06-17 03:27:26.500 EDT [87496] LOG: database system is ready to accept connections
    done
    server started
    [pgsql@10 bin]$ psql -Upgsql postgres
    psql (12.0)
    Type "help" for help.
    postgres=# \q
    [pgsql@10 bin]$ ps -ef | grep postgres
    pgsql      87496       1  0 03:27 ?        00:00:00 /opt/postgresql-12.0/app/bin/postgres -D /opt/postgresql-12.0/data
    pgsql      87498   87496  0 03:27 ?        00:00:00 postgres: checkpointer
    pgsql      87499   87496  0 03:27 ?        00:00:00 postgres: background writer
    pgsql      87500   87496  0 03:27 ?        00:00:00 postgres: walwriter
    pgsql      87501   87496  0 03:27 ?        00:00:00 postgres: autovacuum launcher
    pgsql      87502   87496  0 03:27 ?        00:00:00 postgres: stats collector
    pgsql      87503   87496  0 03:27 ?        00:00:00 postgres: logical replication launcher
    pgsql      87926   85903  0 05:09 pts/1    00:00:00 grep --color=auto postgres
    [pgsql@10 bin]$
    

 

As in the above output command execution, the process for starting the PostgreSQL database server is a success. The process after to prove the existence of the PostgreSQL database server’s service is also a success. The indication is very clear. The successful process for logging in to the PostgreSQL database server. The other one is the existence of the process of PostgreSQL database server in the list of process as part of the output of the ‘ps -ef’ command execution.

Leave a Reply