Subject: Alternate Datafile Location

From: Sam Stearns <[email-masked]>
Date: Fri, 30 Aug 2024 08:41:26 +0930

Howdy,

Here's our filesystems:

[postgres@xxx data]$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                         80G     0   80G   0% /dev
tmpfs                            80G   44M   80G   1% /dev/shm
tmpfs                            80G  716K   80G   1% /run
tmpfs                            80G     0   80G   0% /sys/fs/cgroup
/dev/mapper/rhel-root            28G  4.5G   23G  17% /
tmpfs                            80G  8.0K   80G   1% /tmp
/dev/sda2                      1014M  457M  558M  46% /boot
/dev/sda1                       599M  5.9M  594M   1% /boot/efi
/dev/mapper/pgwalvg-pgwalvol    570G   28K  541G   1% /PG_WAL
/dev/mapper/pgdatavg-pgdatavol  2.3T   28K  2.2T   1% /PGDATA
tmpfs                            16G     0   16G   0% /run/user/1242651222
tmpfs                            16G     0   16G   0% /run/user/1271246868
[postgres@xxx data]$

Our Postgres software install and data directory is on root:

[postgres@xxx data]$ pwd
/var/lib/pgsql/16/data
[postgres@thiludbapql01 data]$ df -h .
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   28G  4.5G   23G  17% /
[postgres@xxx data]$

We want our database datafile storage to be on /PGDATA.  I don't see
anything in the version 16 documentation on how to accomplish this.  I must
be missing it somewhere.  Could someone please advise?

Thanks,

Sam

-- 

*Samuel Stearns*
Lead Database Administrator
*c:* 971 762 6879 | *o:* 503 672 5115 | DAT.com
<https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link>

---

From: Ron Johnson <[email-masked]>
Date: Thu, 29 Aug 2024 19:17:34 -0400

> tmpfs                            16G     0   16G   0% /run/user/1271246868
"df -Ph -x tmpfs -x devtmpfs" would make that look a lot neater.

> We want our database datafile storage to be on /PGDATA.
>

Making the $PGDATA environment variable the same as the directory name is
bound to lead to shell errors.

> I don't see anything in the version 16 documentation on how to accomplish
> this.
>

Where did you look?

How was the instance (aka cluster) created?

> I must be missing it somewhere.  Could someone please advise?
>

https://www.postgresql.org/docs/16/app-initdb.html

-D *directory*
--pgdata=*directory*
<https://www.postgresql.org/docs/16/app-initdb.html#APP-INITDB-OPTION-PGDATA>

This option specifies the directory where the database cluster should be
stored. This is the only information required by initdb, but you can avoid
writing it by setting the PGDATA environment variable, which can be
convenient since the database server (postgres) can find the data directory
later by the same variable.

-- 
Death to America, and butter sauce.
Iraq lobster!

---

From: Sam Stearns <[email-masked]>
Date: Fri, 30 Aug 2024 08:59:16 +0930

Thanks for the advice on the /PGDATA directory naming convention.

I'm not sure how our Linux team created the instance but it is created on
root:

[postgres@thiludbapql01 ~]$ echo $PGDATA
/var/lib/pgsql/16/data
[postgres@thiludbapql01 ~]$ df -h /var/lib/pgsql/16/data
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   28G  4.5G   23G  17% /
[postgres@thiludbapql01 ~]$

That's fine as long as we can create databases with datafile storage on an
alternate filesystem.  I don't see anywhere in the documentation how to do
that.

On Fri, Aug 30, 2024 at 8:47 AM Ron Johnson <[email-masked]> wrote:

> stored. This is the only information required by initdb, but you can
> avoid writing it by setting the PGDATA environment variable, which can be
> convenient since the database server (postgres) can find the data
> directory later by the same variable.
>
> --

-- 

*Samuel Stearns*

---

From: Scott Ribe <[email-masked]>
Date: Thu, 29 Aug 2024 17:33:06 -0600

> On Aug 29, 2024, at 5:29 PM, Sam Stearns <[email-masked]> wrote:
> 
> That's fine as long as we can create databases with datafile storage on an alternate filesystem.  I don't see anywhere in the documentation how to do that.

-D <directory>

---

From: Sam Stearns <[email-masked]>
Date: Fri, 30 Aug 2024 09:10:12 +0930

[postgres@thiludbapql01 ~]$ createdb baptest -D /PGDATA
createdb: error: database creation failed: ERROR:  tablespace "/PGDATA"
does not exist
[postgres@thiludbapql01 ~]$

The directory /PGDATA exists.

[postgres@thiludbapql01 ~]$ df -h /PGDATA
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/pgdatavg-pgdatavol  2.3T   28K  2.2T   1% /PGDATA
[postgres@thiludbapql01 ~]$

On Fri, Aug 30, 2024 at 9:03 AM Scott Ribe <[email-masked]>
wrote:

> > That's fine as long as we can create databases with datafile storage on
> an alternate filesystem.  I don't see anywhere in the documentation how to
> do that.
>
> -D <directory>

-- 

*Samuel Stearns*

---

From: "David G. Johnston" <[email-masked]>
Date: Thu, 29 Aug 2024 16:44:14 -0700

> createdb: error: database creation failed: ERROR:  tablespace "/PGDATA"
Clusters, created using initdb, are what interact with the file system.
Individual databases don’t.  They exist within a cluster.

David J.

---

From: Scott Ribe <[email-masked]>
Date: Thu, 29 Aug 2024 17:47:22 -0600

You should take a step back and read the basic documentation on PG administration. For instance: https://www.postgresql.org/docs/16/creating-cluster.html

We (at least I) have been assuming that you wanted to create a new database cluster, to run a server against that storage, using initdb. Using an already running server to put data elsewhere, using createdb is different, and requires that you create a tablespace first: https://www.postgresql.org/docs/16/manage-ag-tablespaces.html

---

From: Sam Stearns <[email-masked]>
Date: Fri, 30 Aug 2024 09:25:59 +0930

Ok, we were going with the approach of the Postgres software (cluster)
being on the root filesystem then creating databases with data file storage
on an alternate filesystem.

It's sounding like the better approach would be to install the Postgres
software (cluster with initdb) on a filesystem other than root.  Do I have
that right?

On Fri, Aug 30, 2024 at 9:17 AM Scott Ribe <[email-masked]>
wrote:

> You should take a step back and read the basic documentation on PG
> administration. For instance:
> https://www.postgresql.org/docs/16/creating-cluster.html
>
> We (at least I) have been assuming that you wanted to create a new
> database cluster, to run a server against that storage, using initdb. Using
> an already running server to put data elsewhere, using createdb is
> different, and requires that you create a tablespace first:
> https://www.postgresql.org/docs/16/manage-ag-tablespaces.html
>
>

-- 

*Samuel Stearns*

---

From: Ron Johnson <[email-masked]>
Date: Thu, 29 Aug 2024 20:14:28 -0400

> on an alternate filesystem.

PG isn't like SQL Server, where you have a set of catalog databases, and
then can put user databases anywhere you please.

> software (cluster with initdb) on a filesystem other than root.  Do I have

Correct.  Postgresql has tablesspaces, but tools like pg_restore and
PgBackRest are least confusing when all user databases live in the same
$PGDATA/base.  "CREATE DATABASE foo;" handles all that for you.

If you *really* want your user databases spread all over, then one
possibility is that each has its own instance (aka cluster) and port number.

Ubuntu has pg_lsclusters to help manage that, but other OS's require
something like "netstat -an | grep :543[2345]" (which doesn't help if an
instance is shut down.

---

From: Sam Stearns <[email-masked]>
Date: Fri, 30 Aug 2024 09:47:43 +0930

Got it.  Thank you everyone for your help!

Sam

-- 

*Samuel Stearns*

---

From: ji hwan song <[email-masked]>
Date: Fri, 30 Aug 2024 09:18:45 +0900

Inint db로 생성시키면 해당 경로에 시스템 테이블생성이됩니다..이게..사용이 많으면
문제가되겠죠..wall은별도로지정가능하고요..사용자클러스터 디비는..별도로 생성해서 관히하시면 되구요..기본 설치시 생성된디렉토리는
변경이어려우니.추가하려면 별도의 파일시스템을먼들어..링크를 걸어주세요.