Postgresql startup failure - postgresql

I'm attempting to build a box via Fabric on Openstack. Part of the install involves installing and running PostgreSQL.
This command works fine:
$ sudo service postgresql initdb
This command fails:
$ sudo service postgresql start
Log output of failure shows no issues when I run this command:
$ cat /var/lib/pgsql/pgstartup.log
This command shows the following messages:
$ cat /var/lib/pgsql/data/pg_log/postgresql-Wed.log
LOG: could not open configuration file
"/var/lib/pgsql/data/pg_hba.conf": Permission denied
FATAL: could not load pg_hba.conf
My user while executing these commands has the following groups:
vagrant, wheel
My user is in the sudoers list under /etc/sudoers with these permissions:
vagrant ALL=(ALL) NOPASSWD: ALL
perms on pgsql:
[root#integration ~]# ls -ltr /var/lib/pgsql/
total 12
drwx------. 2 postgres postgres 4096 Sep 13 2012 backups
-rw-------. 1 postgres postgres 1152 Jun 19 20:17 pgstartup.log
drwx------. 12 postgres postgres 4096 Jun 19 20:19 data
and sub dir data:
[root#integration ~]# ls -ltr /var/lib/pgsql/data/
total 76
-rw-------. 1 postgres postgres 4 Jun 19 20:17 PG_VERSION
drwx------. 2 postgres postgres 4096 Jun 19 20:17 pg_twophase
drwx------. 2 postgres postgres 4096 Jun 19 20:17 pg_tblspc
drwx------. 2 postgres postgres 4096 Jun 19 20:17 pg_stat_tmp
drwx------. 4 postgres postgres 4096 Jun 19 20:17 pg_multixact
-rw-------. 1 postgres postgres 16886 Jun 19 20:17 postgresql.conf
-rw-------. 1 postgres postgres 1631 Jun 19 20:17 pg_ident.conf
drwx------. 3 postgres postgres 4096 Jun 19 20:17 pg_xlog
drwx------. 2 postgres postgres 4096 Jun 19 20:17 pg_subtrans
drwx------. 2 postgres postgres 4096 Jun 19 20:17 pg_clog
drwx------. 5 postgres postgres 4096 Jun 19 20:17 base
drwx------. 2 postgres postgres 4096 Jun 19 20:17 global
-rw-------. 1 postgres postgres 241 Jun 19 20:17 pg_hba.conf
drwx------. 2 postgres postgres 4096 Jun 19 20:17 pg_log
-rw-------. 1 postgres postgres 57 Jun 19 20:19 postmaster.opts

This turned out to be an selinux issue.
I disabled it and was well. Full config below.
For those of you that arent up on selinux (like me until today) the config can be found in:
/etc/selinux/config
It can also be turned off temporarily like this:
echo 0 > /selinux/enforce
Full config
[root#integration selinux]# cat config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=enforcing
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.

Related

Postgresql and Tablespaces no such file

I am having issues with postgresql 14 and 2 tablespaces:
db=# select * from stock_move;
ERROR: could not open file "pg_tblspc/32192/PG_14_202107181/16384/32197": No such file or directory
db=#
db=# select * from stock_picking;
ERROR: could not open file "pg_tblspc/32191/PG_14_202107181/16384/32194": No such file or directory
db=#
Everything was normal, but we decided to scale the server and when we restarted it came up with the error described above. There is also another tablespace, but it is working normally.
Tablespaces:
postgres#database-server:/mnt$ ls -l
total 12
drwx------ 4 postgres postgres 4096 Jan 9 05:33 stock_move
drwx------ 4 postgres postgres 4096 Jan 9 05:34 stock_move_line
drwx------ 4 postgres postgres 4096 Jan 9 05:34 stock_picking
postgres#database-server:/mnt$
These tablespaces are put in a partition for each one, each partition is mounted in /etc/fstab
/dev/nvme1n1 /mnt/stock_move ext4 defaults 0 1
/dev/nvme2n1 /mnt/stock_move_line ext4 defaults 0 1
/dev/nvme3n1 /mnt/stock_picking ext4 defaults 0 1
It seems that postgres tries to search for these files but they do not exist (32197, 32194).
root#database-server:/mnt/stock_picking/PG_14_202107181/16384# ll
total 1939392
drwx------ 2 postgres postgres 4096 Jan 13 01:06 ./
drwx------ 3 postgres postgres 4096 Jan 9 05:39 ../
-rw------- 1 postgres postgres 1073741824 Jan 11 17:26 32197
-rw------- 1 postgres postgres 911597568 Jan 12 00:01 32197.1
-rw------- 1 postgres postgres 507904 Jan 11 23:16 32197_fsm
-rw------- 1 postgres postgres 65536 Jan 11 17:17 32197_vm
-rw------- 1 postgres postgres 0 Jan 9 05:39 32198
-rw------- 1 postgres postgres 8192 Jan 9 05:39 32199
root#database-server:/mnt/stock_picking/PG_14_202107181/16384#
and
root#database-server:/mnt/stock_move/PG_14_202107181/16384# ll
total 1200276
drwx------ 2 postgres postgres 4096 Jan 13 01:10 ./
drwx------ 3 postgres postgres 4096 Jan 9 05:36 ../
-rw------- 1 postgres postgres 38191104 Jan 12 00:01 32194
-rw------- 1 postgres postgres 32768 Jan 11 19:26 32194_fsm
-rw------- 1 postgres postgres 8192 Jan 9 17:39 32194_vm
-rw------- 1 postgres postgres 0 Jan 9 05:36 32195
-rw------- 1 postgres postgres 8192 Jan 9 05:36 32196
-rw------- 1 postgres postgres 1073741824 Jan 11 16:55 32203
-rw------- 1 postgres postgres 116727808 Jan 12 00:01 32203.1
-rw------- 1 postgres postgres 311296 Jan 11 23:43 32203_fsm
-rw------- 1 postgres postgres 40960 Jan 11 23:47 32203_vm
-rw------- 1 postgres postgres 0 Jan 9 05:44 32204
-rw------- 1 postgres postgres 8192 Jan 9 05:44 32205
root#database-server:/mnt/stock_move/PG_14_202107181/16384#
and pg_tblspc links
root#database-server:/var/lib/postgresql/14/main/pg_tblspc# ll
total 8
drwx------ 2 postgres postgres 4096 Jan 9 05:34 ./
drwx------ 19 postgres postgres 4096 Jan 13 01:15 ../
lrwxrwxrwx 1 postgres postgres 18 Jan 9 05:33 32191 -> /mnt/stock_picking/
lrwxrwxrwx 1 postgres postgres 15 Jan 9 05:34 32192 -> /mnt/stock_move/
lrwxrwxrwx 1 postgres postgres 20 Jan 9 05:34 32193 -> /mnt/stock_move_line/
root#database-server:/var/lib/postgresql/14/main/pg_tblspc#
From here on I don't know what to do.
The only good explanation is that someone or something deleted the files that make up this table. This is the time to restore your backup.

PostgreSQL not creating new WAL at archive_timeout interval

I have set archive_timeout to 300 seconds (5 min). Yet WAL files remain open for a very long time (hours). My postgresql.conf file includes:
archive_mode = on
archive_timeout = 300
archive_command = '/usr/local/internal/pgarchive.sh "%p" "%f"'
restore_command = '/usr/local/internal/pgrestore.sh "%f" "%p"'
wal_level = logical
And I don't see any errors on postgresql 13.5 startup. The pgsql 13.5 pg_wal directory contains:
drwx------ 3 postgres postgres 4096 Oct 19 15:51 .
drwx------ 19 postgres postgres 4096 Oct 19 15:31 ..
-rw------- 1 postgres postgres 348 Oct 19 15:31 000000010000000000000097.00000028.backup
-rw------- 1 postgres postgres 16777216 Oct 19 15:51 00000001000000000000009A
-rw------- 1 postgres postgres 16777216 Oct 19 15:56 00000001000000000000009B
-rw------- 1 postgres postgres 16777216 Oct 19 15:31 00000001000000000000009C
-rw------- 1 postgres postgres 16777216 Oct 19 15:36 00000001000000000000009D
-rw------- 1 postgres postgres 16777216 Oct 19 15:41 00000001000000000000009E
drwx------ 2 postgres postgres 4096 Oct 19 15:56 archive_status
And the archive directory contains:
drwxrwxrwx 2 postgres postgres 4096 Oct 19 15:56 .
drwxrwxrwx 4 root root 4096 Oct 18 20:57 ..
-rw------- 1 postgres postgres 16777216 Oct 19 15:30 000000010000000000000094
-rw------- 1 postgres postgres 16777216 Oct 19 15:30 000000010000000000000095
-rw------- 1 postgres postgres 16777216 Oct 19 15:31 000000010000000000000096
-rw------- 1 postgres postgres 16777216 Oct 19 15:31 000000010000000000000097
-rw------- 1 postgres postgres 348 Oct 19 15:31 000000010000000000000097.00000028.backup
-rw------- 1 postgres postgres 16777216 Oct 19 15:36 000000010000000000000098
-rw------- 1 postgres postgres 16777216 Oct 19 15:41 000000010000000000000099
-rw------- 1 postgres postgres 16777216 Oct 19 15:51 00000001000000000000009A
-rw------- 1 postgres postgres 16777216 Oct 19 15:56 00000001000000000000009B
As you can see the pg_wal directory contains files 9C, 9D, and 9E which have not yet been archived. (9D and 9E are probably recycled old wal's not yet used as per comment below). But 9B has been open for an hour or more. Why is it not being archived?
Why is postgresql not forcing a new logfile every 5 min?

DOCKER container with postgres, WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted

I have a DOCKER container built from a few different images using a .yml, Dockerfile(s), etc. Everything builds and runs fine so far, except for this one issue that I am seeing mentioned in the title:
index-db_1 | 2021-02-22 23:18:33.388 UTC [31] WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted
That Database Index is mapped to a Folder on the host in the root of the Docker Package, and everything else seems to work fine as far as the database is concerned. I am using a Mac, but if I list permission from CLI for the DB folder I get:
-rw-------# 1 sscotti staff 3 Feb 22 11:01 PG_VERSION
drwx------# 6 sscotti staff 192 Feb 22 11:54 base
drwx------# 60 sscotti staff 1920 Feb 22 16:00 global
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_commit_ts
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_dynshmem
-rw-------# 1 sscotti staff 4782 Feb 22 11:02 pg_hba.conf
-rw-------# 1 sscotti staff 1636 Feb 22 11:01 pg_ident.conf
drwx------# 5 sscotti staff 160 Feb 22 17:46 pg_logical
drwx------# 4 sscotti staff 128 Feb 22 11:01 pg_multixact
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_notify
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_replslot
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_serial
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_snapshots
drwx------# 2 sscotti staff 64 Feb 22 16:00 pg_stat
drwx------# 5 sscotti staff 160 Feb 22 17:50 pg_stat_tmp
drwx------# 3 sscotti staff 96 Feb 22 11:01 pg_subtrans
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_tblspc
drwx------# 2 sscotti staff 64 Feb 22 11:01 pg_twophase
drwx------# 4 sscotti staff 128 Feb 22 11:01 pg_wal
drwx------# 3 sscotti staff 96 Feb 22 11:01 pg_xact
-rw-------# 1 sscotti staff 88 Feb 22 11:01 postgresql.auto.conf
-rw-------# 1 sscotti staff 28073 Feb 22 11:01 postgresql.conf
-rw-------# 1 sscotti staff 36 Feb 22 16:00 postmaster.opts
-rw------- 1 sscotti staff 94 Feb 22 16:00 postmaster.pid
pg_stat folder is actually empty.
and pg_stat_temp has:
-rw------- 1 sscotti staff 1952 Feb 22 17:54 db_0.stat
-rw------- 1 sscotti staff 20360 Feb 22 17:54 db_13395.stat
-rw------- 1 sscotti staff 1151 Feb 22 17:54 global.stat
The .yml file has this:
index-db:
image: postgres
restart: unless-stopped
volumes:
- ./OrthancIndex:/var/lib/postgresql/data
Is that something that can just be ignored given that it is a Docker container.
Adding a comment about the same setup on UBUNTU.
Database Folder:
drwx------ 19 systemd-coredump root 4096 Jun 30 13:12 OrthancIndex
Database:
drwx------ 6 systemd-coredump systemd-coredump 4096 Jun 11 13:00 base
drwx------ 2 systemd-coredump systemd-coredump 4096 Jun 30 13:12 global
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_commit_ts
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_dynshmem
-rw------- 1 systemd-coredump systemd-coredump 4782 Mar 12 16:12 pg_hba.conf
-rw------- 1 systemd-coredump systemd-coredump 1636 Mar 12 16:12 pg_ident.conf
drwx------ 4 systemd-coredump systemd-coredump 4096 Jul 1 13:27 pg_logical
drwx------ 4 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_multixact
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_notify
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_replslot
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_serial
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_snapshots
drwx------ 2 systemd-coredump systemd-coredump 4096 Jun 30 13:12 pg_stat
drwx------ 2 systemd-coredump systemd-coredump 4096 Jul 1 13:29 pg_stat_tmp
drwx------ 2 systemd-coredump systemd-coredump 4096 Jun 24 21:04 pg_subtrans
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_tblspc
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_twophase
-rw------- 1 systemd-coredump systemd-coredump 3 Mar 12 16:12 PG_VERSION
drwx------ 3 systemd-coredump systemd-coredump 4096 Jul 1 12:37 pg_wal
drwx------ 2 systemd-coredump systemd-coredump 4096 Mar 12 16:12 pg_xact
-rw------- 1 systemd-coredump systemd-coredump 88 Mar 12 16:12 postgresql.auto.conf
-rw------- 1 systemd-coredump systemd-coredump 28073 Mar 12 16:12 postgresql.conf
-rw------- 1 systemd-coredump systemd-coredump 36 Jun 30 13:12 postmaster.opts
-rw------- 1 systemd-coredump systemd-coredump 94 Jun 30 13:12 postmaster.pid
pg_stat_temp
-rw------- 1 systemd-coredump systemd-coredump 2660 Jul 1 13:30 db_0.stat
-rw------- 1 systemd-coredump systemd-coredump 31157 Jul 1 13:30 db_13395.stat
-rw------- 1 systemd-coredump systemd-coredump 1151 Jul 1 13:30 global.stat
I actually get the same error on UBUNTU:
postgres_index-db_1 | 2021-07-01 18:06:45.140 UTC [266] WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted
postgres_index-db_1 | 2021-07-01 18:13:45.583 UTC [273] WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted
postgres_index-db2_1 | 2021-07-01 18:19:43.716 UTC [282] WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted
postgres_index-db2_1 | 2021-07-01 18:21:43.749 UTC [284] WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted
Although here the user and group are systemd-coredump.
Simple and dumb
I've tried another way: leaving the temporary file in container, where you need not deal with user permissions - and that worked. I've changed with inline command the Postgresql config in docker-compose.yml:
postgresdb:
image: 'postgres'
command: postgres -c stats_temp_directory=/tmp
.
.
.
This is quick and simple solution, but it does not set multiple postgres config parameters. Through the time I found another solution.
Advanced (added on 22/01/14 09:00PM CET)
The advanced solution consists in these steps:
Prepare your desired postgresql.conf - here you can set whichever postgres server parameters you need. The necessary one for solving problem in this thread is:
stats_temp_directory = '/tmp/stat_temporary'
Create bash file, that you will start using docker-entrypoint-initdb.d in docker-compose.yml with which you will copy your configuration to postgres config directory in docker and create your temporary folder.
If you setup the docker-compose.yml this way:
postgres-db:
image: postgres
container_name: 'dbcontainer'
volumes:
# point to your postgres init scripts (folder or file)
- $PWD/db_init:/docker-entrypoint-initdb.d
# if you need persistent data, point data to local folder
- $PWD/database:/var/lib/postgresql/data
# if you want some data restore, point to your backup
- $PWD/db_backup:/db_backup
# point docker to directory with your config
- $PWD/db_config:/db_config
Then database will init with scripts in db_init directory. (Or you can point directly to one sql or bash file. If you point to directory, init script will run all files in alphabetical order.
Your file with init bash script, that you will place in your db_init folder can look like this and will be started inside docker:
echo starting script...
# create temporary directory for postgres in docker
mkdir /tmp/stat_temporary
# copy your postgresql.conf to postgresql config location in docker
cp /db_config/postgresql.conf var/lib/postgresql/data/postgresql.conf
# alternatively you can run any pg script you need, here I restore backup to new database
pg_restore -U postgres -v -Fc -d analysis /db_backup/analysis_backup.bak
echo finished
After this script is run, the database service on docker is restarted and loads with new parameters you set up.
Writing this down as much for my future self as to answer the question: looking over at the documentation and taking #Fide's solution above, the following seems to work on MacOSX Monterey running a PostGIS image (which is just built on top of the Postgres images)...
Note that this is taken from a shell script to launch a Postgres+PostGIS image with a persistent local store on the host system:
WORK_DIR="$HOME/Documents/git/project"
DOCKER_NM="postgis"
DOCKER_IMG="postgis/postgis"
POSTGRES_PWD="XXX"
PORT_NO="5432"
docker run --name $DOCKER_NM \
-e POSTGRES_PASSWORD=$POSTGRES_PWD \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v "$WORK_DIR/data/postgres":/var/lib/postgresql/data \
-p "$PORT_NO:5432" -d $DOCKER_IMG \
postgres -c stats_temp_directory=/tmp
If you were running from the Terminal directly (i.e. not via zsh/bash) then you'd need to export each of the parameters instead of just specifying them.
With this the global.stat error seems to have gone away... though I should note that I've only just figured this out and have been running the container for less than 60 minutes. The alternative would be to follow the instructions on that page for creating and using a persistent custom conf file (extending #SScotti's comment above) as mounted from the local FS:
-v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf
Mounting with a local volume requires user mapping within the postgres container. There is a section in the https://hub.docker.com/_/postgres README titled Arbitrary --user Notes which describes solutions to the problem.
One recommendation listed is to use a docker volume, initialize the contents of volume mounted, shutdown the container image, run a recursive ownership change and then restart the container normally.
$ docker volume create pgdata
$ docker run -it --rm -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=mysecretpassword postgres
The files belonging to this database system will be owned by user "postgres".
...
( once it's finished initializing successfully and is waiting for connections, stop it )
$ docker run -it --rm -v pgdata:/var/lib/postgresql/data bash chown -R 1000:1000 /var/lib/postgresql/data
$ docker run -it --rm --user 1000:1000 -v pgdata:/var/lib/postgresql/data postgres
Not an answer to the original question, but hence the lack of reputation points I can't add my two cents as a comment. So here is a working docker-compose.yml file using the tmpfs solution as an answer (for #SScotti and anyone else, who wants to try this without cluttering the default command or config):
Environment file (.env):
POSTGRES_VERSION=13.6
DATABASE_NAME=dbname
DATABASE_USER=dbuser
DATABASE_PASS=dbpass
Compose file (docker-compose.yml):
version: '3.6'
services:
postgres:
image: postgres:${POSTGRES_VERSION}-alpine
restart: always
container_name: postgres
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASS}
volumes:
- /etc/timezone:/etc/timezone:ro
- .volumes/pgdata:/var/lib/postgresql/data
- type: tmpfs
target: /var/lib/postgresql/data/pg_stat_tmp
tmpfs:
# 256 Mb - beware the default, it's infinity(!)
size: 268435456
Edit: Note on first run
On first run, postgres tries to initialise the database and fails, because of non empty data directory (the existing/mounted tmpfs pg_stats_tmp in the data directory).
To avoid that, you have to run the service at first without the tmpfs section in the compose file, and when postgres finished initialising take the service down and enable the tmpfs section in the compose file before bringen up the service again.
I ran into this same problem - Looked into it in detail and finally found a simple solution.
First some basic background - your pg_stat_temp_directory location is a subdirectory under your data_directory.
show data_directory ;
If it returns the default value for Unix based systems like
/var/lib/postgresql/data
And your command to show stats_temp
show stats_temp_directory ;
Also returns the default value
pg_stat_tmp
Then your full path to stats_temp_directory is /var/lib/postgresql/data/pg_stat_tmp
I was using bind mapping to map the PG container data files to my host so that I can keep the data even after the container is stopped.
docker run -d --name gutlo-postgres -p 5434:5432 -v /data/postgres-docker/14:/var/lib/postgresql/data -e POSTGRES_PASSWORD=<PASSWORD> postgres:latest
Now everytime I start the container, it automatically creates files under stats_temp directory and once that it has a problem with is called global.stat
ls -la global.stat
-rw------- 1 ssengupta admin 3648 May 13 16:24 global.stat
ls -la . | head -2 | tail -1
drwxr-xr-x# 14 ssengupta admin 448 May 13 16:26 .
Now you see, where is the problem. The container is able to create the file global.stat ( its directory file "." has open rwx permissions. But the file itself is created without open permission hence my container cannot access its own created file.
From docker manual, there is a concept of temp mount. So simply create a temp mount point for the stats_temp directory and the docker will use tempfs / RAM - that eliminates permission problem.
--mount type=tmpfs,destination=/var/lib/postgresql/data/pg_stat_tmp
In addition it would improve performance too although you may not detect it.
My attempts...
Attach to the db container:
docker compose run --no-deps -it db bash
and do ls on the data directory:
root#dfc6399e2981:/var/lib/postgresql/data# ls -l
total 60
drwx------ 7 root root 224 Sep 8 23:38 base
drwx------ 60 root root 1920 Sep 8 23:38 global
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_commit_ts
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_dynshmem
-rw------- 1 postgres postgres 4821 Sep 8 23:35 pg_hba.conf
-rw------- 1 postgres postgres 1636 Sep 8 23:35 pg_ident.conf
drwx------ 5 root root 160 Sep 8 23:43 pg_logical
drwx------ 4 postgres postgres 128 Sep 8 23:35 pg_multixact
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_notify
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_replslot
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_serial
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_snapshots
drwx------ 2 root root 64 Sep 8 23:35 pg_stat
drwx------ 6 root root 192 Sep 9 00:20 pg_stat_tmp
drwx------ 3 postgres postgres 96 Sep 8 23:35 pg_subtrans
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_tblspc
drwx------ 2 postgres postgres 64 Sep 8 23:35 pg_twophase
-rw------- 1 postgres postgres 3 Sep 8 23:35 PG_VERSION
drwx------ 4 postgres postgres 128 Sep 8 23:35 pg_wal
drwx------ 3 postgres postgres 96 Sep 8 23:35 pg_xact
-rw------- 1 postgres postgres 88 Sep 8 23:35 postgresql.auto.conf
-rw------- 1 postgres postgres 28835 Sep 8 23:35 postgresql.conf
-rw------- 1 root root 36 Sep 8 23:35 postmaster.opts
-rw------- 1 postgres postgres 94 Sep 8 23:35 postmaster.pid
Obviously, note that some things are owned by root. Also, compare and contrast, from outside the container I see this:
% ls -l db/data
total 120
-rw-------# 1 pedz staff 3 Sep 8 18:35 PG_VERSION
drwx------# 7 pedz staff 224 Sep 8 18:38 base
drwx------# 60 pedz staff 1920 Sep 8 18:38 global
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_commit_ts
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_dynshmem
-rw-------# 1 pedz staff 4821 Sep 8 18:35 pg_hba.conf
-rw-------# 1 pedz staff 1636 Sep 8 18:35 pg_ident.conf
drwx------# 5 pedz staff 160 Sep 8 18:43 pg_logical
drwx------# 4 pedz staff 128 Sep 8 18:35 pg_multixact
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_notify
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_replslot
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_serial
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_snapshots
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_stat
drwx------# 6 pedz staff 192 Sep 8 19:27 pg_stat_tmp
drwx------# 3 pedz staff 96 Sep 8 18:35 pg_subtrans
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_tblspc
drwx------# 2 pedz staff 64 Sep 8 18:35 pg_twophase
drwx------# 4 pedz staff 128 Sep 8 18:35 pg_wal
drwx------# 3 pedz staff 96 Sep 8 18:35 pg_xact
-rw-------# 1 pedz staff 88 Sep 8 18:35 postgresql.auto.conf
-rw-------# 1 pedz staff 28835 Sep 8 18:35 postgresql.conf
-rw-------# 1 pedz staff 36 Sep 8 18:35 postmaster.opts
-rw-------# 1 pedz staff 94 Sep 8 18:35 postmaster.pid
So, while in the db container I did:
chown -R postgres:postgres /var/lib/postgresql/data
I can now see updates into pg_stat_tmp from outside of the container and it has been up for 30 minutes with no Warning messages so I think its "fixed" although I don't know enough about Docker's images to know for sure that the issue won't come back later. Perhaps someone with more Docker knowledge can chime in.
Update: this does NOT fix the issue. Rather than delete this answer I think I will leave it here. It is odd that some files are owned by root. And even after I did chown -R there are still some files owned by root:
root#ec24992481d1:/var/lib/postgresql# find . -user 0 -exec ls -ld {} +
-rw------- 1 root root 8 Sep 9 00:34 ./data/pg_logical/replorigin_checkpoint
-rw------- 1 root root 2225 Sep 9 01:12 ./data/pg_stat_tmp/db_0.stat
-rw------- 1 root root 6665 Sep 9 01:12 ./data/pg_stat_tmp/db_13757.stat
-rw------- 1 root root 7035 Sep 9 01:12 ./data/pg_stat_tmp/db_16384.stat
-rw------- 1 root root 94 Sep 9 00:29 ./data/postmaster.pid
root#ec24992481d1:/var/lib/postgresql#
This might be a clue for others more savvy with Docker.

postgres cannot access the server configuration file: Permission denied

This is baffling me (trying to set up postgres on MacOS Lion):
delirium:$ sudo ls -al /usr/local/pgsql/data/
total 64
drwx------ 15 postgres postgres 510 25 May 10:36 .
drwxr-xr-x# 13 103 103 442 25 May 10:35 ..
-rw------- 1 postgres postgres 4 25 May 10:36 PG_VERSION
drwx------ 5 postgres postgres 170 25 May 10:36 base
drwx------ 41 postgres postgres 1394 25 May 10:36 global
drwx------ 3 postgres postgres 102 25 May 10:36 pg_clog
-rw------- 1 postgres postgres 3652 25 May 10:36 pg_hba.conf
-rw------- 1 postgres postgres 1631 25 May 10:36 pg_ident.conf
drwx------ 4 postgres postgres 136 25 May 10:36 pg_multixact
drwx------ 2 postgres postgres 68 25 May 10:36 pg_stat_tmp
drwx------ 3 postgres postgres 102 25 May 10:36 pg_subtrans
drwx------ 2 postgres postgres 68 25 May 10:36 pg_tblspc
drwx------ 2 postgres postgres 68 25 May 10:36 pg_twophase
drwx------ 4 postgres postgres 136 25 May 10:36 pg_xlog
-rwxr-xr-x 1 postgres postgres 16879 25 May 10:36 postgresql.conf
delirium:$ /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
postgres cannot access the server configuration file "/usr/local/pgsql/data/postgresql.conf": Permission denied
Doesn't the output show that postgres is the owner of postgresql.conf? So why can't it access the file?
What can I do?
The quickest way to diagnose a permission denied error is to login (su or sudo) as that user and try to cat the file.
If this is permission denied and not file not found, I'm guessing the postgres user has not got execute permissions on one of the parent directories, it could be any of the 3: /usr/local/pgsql/
sudo -u postgres /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
...assuming postgres was set up with default user postgres

Where does PostgreSQL store the database?

Where are the files for a PostgreSQL database stored?
To see where the data directory is, use this query.
show data_directory;
To see all the run-time parameters, use
show all;
You can create tablespaces to store database objects in other parts of the filesystem. To see tablespaces, which might not be in that data directory, use this query.
SELECT *, pg_tablespace_location(oid) FROM pg_tablespace;
On Windows7 all the databases are referred by a number in the file named pg_database under C:\Program Files (x86)\PostgreSQL\8.2\data\global. Then you should search for the folder name by that number under C:\Program Files (x86)\PostgreSQL\8.2\data\base. That is the content of the database.
As suggested in "PostgreSQL database default location on Linux", under Linux you can find out using the following command:
ps aux | grep postgres | grep -- -D
Open pgAdmin and go to Properties for specific database. Find OID and then open directory
<POSTGRESQL_DIRECTORY>/data/base/<OID>
There should be your DB files.
Under my Linux installation, it's here: /var/lib/postgresql/8.x/
You can change it with initdb -D "c:/mydb/"
Everyone already answered but just for the latest updates. If you want to know where all the configuration files reside then run this command in the shell
SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
I'd bet you're asking this question because you've tried pg_ctl start and received the following error:
pg_ctl: no database directory specified and environment variable PGDATA unset
In other words, you're looking for the directory to put after -D in your pg_ctl start command.
In this case, the directory you're looking for contains these files.
PG_VERSION pg_dynshmem pg_multixact
pg_snapshots pg_tblspc postgresql.conf
base pg_hba.conf pg_notify
pg_stat pg_twophase postmaster.opts
global pg_ident.conf pg_replslot
pg_stat_tmp pg_xlog postmaster.pid
pg_clog pg_logical pg_serial
pg_subtrans postgresql.auto.conf server.log
You can locate it by locating any of the files and directories above using the search provided with your OS.
For example in my case (a HomeBrew install on Mac OS X), these files are located in /usr/local/var/postgres. To start the server I type:
pg_ctl -D /usr/local/var/postgres -w start
... and it works.
Postgres stores data in files in its data directory. Follow the steps below to go to a database and its files:
The database corresponding to a postgresql table file is a directory. The location of the entire data directory can be obtained by running SHOW data_directory.
in a UNIX like OS (eg: Mac) /Library/PostgreSQL/9.4/data
Go inside the base folder in the data directory which has all the database folders: /Library/PostgreSQL/9.4/data/base
Find the database folder name by running (Gives an integer. This is the database folder name):
SELECT oid from pg_database WHERE datname = <database_name>;
Find the table file name by running (Gives an integer. This is the file name):
SELECT relname, relfilenode FROM pg_class WHERE relname = <table_name>;
This is a binary file. File details such as size and creation date time can be obtained as usual. For more info read this SO thread
The location of specific tables/indexes can be adjusted by TABLESPACEs:
CREATE TABLESPACE dbspace LOCATION '/data/dbs';
CREATE TABLE something (......) TABLESPACE dbspace;
CREATE TABLE otherthing (......) TABLESPACE dbspace;
On Mac: /Library/PostgreSQL/9.0/data/base
The directory can't be entered, but you can look at the content via: sudo du -hc data
picmate's answer is right. on windows the main DB folder location is (at least on my installation)
C:\PostgreSQL\9.2\data\base\
and not in program files.
his 2 scripts, will give you the exact directory/file(s) you need:
SELECT oid from pg_database WHERE datname = <database_name>;
SELECT relname, relfilenode FROM pg_class WHERE relname = <table_name>;
mine is in datname 16393 and relfilenode 41603
On Windows, the PGDATA directory that the PostgresSQL docs describe is at somewhere like C:\Program Files\PostgreSQL\8.1\data. The data for a particular database is under (for example) C:\Program Files\PostgreSQL\8.1\data\base\100929, where I guess 100929 is the database number.
I'm running postgres (9.5) in a docker container (on CentOS, as it happens), and as Skippy le Grand Gourou mentions in a comment above, the files are located in /var/lib/postgresql/data/.
$ docker exec -it my-postgres-db-container bash
root#c7d61efe2a5d:/# cd /var/lib/postgresql/data/
root#c7d61efe2a5d:/var/lib/postgresql/data# ls -lh
total 56K
drwx------. 7 postgres postgres 71 Apr 5 2018 base
drwx------. 2 postgres postgres 4.0K Nov 2 02:42 global
drwx------. 2 postgres postgres 18 Dec 27 2017 pg_clog
drwx------. 2 postgres postgres 6 Dec 27 2017 pg_commit_ts
drwx------. 2 postgres postgres 6 Dec 27 2017 pg_dynshmem
-rw-------. 1 postgres postgres 4.4K Dec 27 2017 pg_hba.conf
-rw-------. 1 postgres postgres 1.6K Dec 27 2017 pg_ident.conf
drwx------. 4 postgres postgres 39 Dec 27 2017 pg_logical
drwx------. 4 postgres postgres 36 Dec 27 2017 pg_multixact
drwx------. 2 postgres postgres 18 Nov 2 02:42 pg_notify
drwx------. 2 postgres postgres 6 Dec 27 2017 pg_replslot
drwx------. 2 postgres postgres 6 Dec 27 2017 pg_serial
drwx------. 2 postgres postgres 6 Dec 27 2017 pg_snapshots
drwx------. 2 postgres postgres 6 Sep 16 21:15 pg_stat
drwx------. 2 postgres postgres 63 Nov 8 02:41 pg_stat_tmp
drwx------. 2 postgres postgres 18 Oct 24 2018 pg_subtrans
drwx------. 2 postgres postgres 6 Dec 27 2017 pg_tblspc
drwx------. 2 postgres postgres 6 Dec 27 2017 pg_twophase
-rw-------. 1 postgres postgres 4 Dec 27 2017 PG_VERSION
drwx------. 3 postgres postgres 92 Dec 20 2018 pg_xlog
-rw-------. 1 postgres postgres 88 Dec 27 2017 postgresql.auto.conf
-rw-------. 1 postgres postgres 21K Dec 27 2017 postgresql.conf
-rw-------. 1 postgres postgres 37 Nov 2 02:42 postmaster.opts
-rw-------. 1 postgres postgres 85 Nov 2 02:42 postmaster.pid
A single Terminal command: pg_lsclusters, (using Ubuntu)
What you need is under Data directory:
Ver Cluster Port Status Owner Data directory Log file
10 main 5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
11 main 5433 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log