5

I have VM with ubuntu 18.04 server installed. I have installed postgres 10 from the repo.

Default installation. I made some connection count related changes in postgres.conf and connection security related changes in pg_hba.conf.

The install directories are all the default, including the data directory.

Server is running. I was able to create databases and access the same from java client.

But when I start psql from the same VM, it throws the following error.

Error: Invalid data directory

Just psql without any parameters also gives this error.

People have faced this error when they have tried to change the data directory which is not the case for me. Data directory is the default and it is owned by the postgres user.

ubuntu@ubuntu:~$ ls -l /var/lib/postgresql/10/
total 4
drwx------ 20 postgres postgres 4096 Jun 12 02:54 main

Any idea what could be the cause or where should I look for detailed error for psql client?

2
  • 1
    psql command-line client or postmaster server process? what did you try that provoked that error message?
    – Jasen
    Commented Jun 12, 2018 at 10:14
  • which repo? ubuntu or postgres?
    – Jasen
    Commented Jun 12, 2018 at 10:17

3 Answers 3

13

Error: Invalid data directory

This happens when pg_wrapper is unable to figure out the data directory from the configuration. pg_wrapper is the layer on top of postgres that can juggle between several live PostgreSQL instances on debian-based systems. As installed by the postgresql-client package, psql is a link to pg_wrapper on such systems:

/usr/bin/psql -> ../share/postgresql-common/pg_wrapper

Since you mention slightly modifying postgresql.conf, make sure you didn't change the permissions inside /etc/postgresql/, including postgresql.conf. That looks like the most plausible change that would immediately lead to the mentioned error. The next one would be that your changes are syntactically wrong, in a way that pg_wrapper would no longer be able to parse the file.

3
  • 3
    Thanks, you are right. I was using ansible to update the postgres.conf. It changed the permission from 0644 to 0640. Resetting back the permission fixed the issue. Somehow this was not happening on ubuntu 16.04. Is there anyway to increase the verbosity of psql command so that I could have got this hint. Commented Jun 12, 2018 at 16:21
  • Indeed, my user (as opposed to postgres user) didn't have read access to postgresql.conf. The culprit is ANXS.postgresql.
    – x-yuri
    Commented Feb 11, 2019 at 19:54
  • A couple of links that show the stacktrace.
    – x-yuri
    Commented Feb 11, 2019 at 19:55
0

This happened to me when i logged in as root and used sed to change postgresql.conf, which resulted in that new conf file being owned by root instead of postgres. postgresql.conf and everything else in the director should have the following permissions:

total 92
drwxr-xr-x 3 postgres postgres  4096 Apr  5 18:56 .
drwxr-xr-x 3 postgres postgres  4096 Apr  5 18:11 ..
drwxr-xr-x 2 postgres postgres  4096 Apr  5 18:11 conf.d
-rw-r--r-- 1 postgres postgres   315 Apr  5 18:11 environment
-rw-r--r-- 1 postgres postgres   143 Apr  5 18:11 pg_ctl.conf
-rw-r----- 1 postgres postgres  4933 Apr  5 18:11 pg_hba.conf
-rw-r----- 1 postgres postgres  1636 Apr  5 18:11 pg_ident.conf
-rw-r----- 1 postgres postgres 26889 Apr  5 18:55 postgresql.conf
-rw-r--r-- 1 postgres postgres   317 Apr  5 18:11 start.conf

0

I found the source of the issue thanks to @x-yuri comment above to the source code.

It is all to do with this block of code:

# Read a 'var = value' style configuration file from a cluster configuration
# directory (with /etc/postgresql-common/<file name> as fallback) and return a
# hash with the values. Error out if the file cannot be read.
# Arguments: <version> <cluster> <config file name>
# Returns: hash (empty if the file does not exist)
sub read_cluster_conf_file {
    my $fname = "$confroot/$_[0]/$_[1]/$_[2]";
    -e $fname or $fname = "$common_confdir/$_[2]";
    my %conf = read_conf_file $fname;

    if ($_[0] >= 9.4 and $_[2] eq 'postgresql.conf') { # merge settings changed by ALTER SYSTEM
        # data_directory cannot be changed by ALTER SYSTEM
        *my $data_directory = $conf{data_directory} || "/var/lib/postgresql/$_[0]/$_[1]";*
        my %auto_conf = read_conf_file "$data_directory/postgresql.auto.conf";
        foreach my $guc (keys %auto_conf) {
            $conf{$guc} = $auto_conf{$guc};
        }
    }

    return %conf;
}

If your configuration file directory has this format: /etc/postgresql/15/main/postgresql.conf

It will use the subdirectories to construct a non-existent data directory!

If you add your real data directory to your postgresql.conf, the error should disappear! (it did for me)

Then when I changed my configuration file direction to this format: /etc/postgresql/postgresql.conf The error also disappeared!

Some context- I am working with postgres:15 as a docker image and was facing Error: Invalid data directory for cluster 15 main when I manually updated my config file location to above.

Many thanks for your help x-yuri!

1
  • The cause of this issue is definitely something to do with wanting to use the old postgres file directory structure, as the code uses that to update your data directory. By omitting the excess subdirectories, I avoided the issue reproducibly!
    – yyy
    Commented Sep 14, 2023 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.