<commit_before>---
layout: post
title: "Setting up Postgres database's location on NAS"
excerpt: On MacOS using Autofs
tags: [postgres, macos, nas, wd]
comments: true
---

Network storages are really handy. Much more disk space,
RAID-0, fast and reliable shared storage across family. They have decent small CPUs on the board, you can manage the time table when storage is enabled and have mobile apps. I wish I had NAS before.

I bought one from Western Digital (MyCloud EX2Ultra to be specific), and
one of the main reason for that is being able to work with a huge database.
In my current case, it's Postgres, which runs on my Mac with 256GB, so any database larger than XX Gb is large enough. The cloud solution is also possible solution. But what about 2TB database?

My first attempt was about mount drive manually and create the database there:

```
mount -t smbfs smb://MyCloudEX2Ultra/Public/db db
initdb --debug -D nas/db/postgres
```

And it failed. It couldn't make symlinks on remote drive:

```
could not link file "pg_xlog/xlogtemp.71138" to "pg_xlog/000000010000000000000001": Operation not supported
```

After rebooting in safe mode (Cmd+R) I run:

```
csrutil disable
reboot
```

That needed because by default Mac has System Integrity Protection and it won't allow editing system files. You can disable that:

```
sudo nano /System/Library/LaunchDaemons/com.apple.smbd.plist
<key>ProgramArguments</key>
<array>
     <string>/usr/sbin/smbd</string>
     <string>--no-symlinks</string>
     <string>false</string>
</array>
```

It didn't help either.

I choose easier way, just store logs locally.

```
initdb --debug -D nas/db/postgres -X db_wal
```

And that worked. Now I could run postgres like
```
pg_ctl -D ~/nas/db/postgres start &
```

### Auto mounting

After successfully creating db on the remote shared disk all I needed is setting up automounting directory on the system startup using `autofs`.

It requires editing two files. **First, is /etc/auto_master**

```
sudo nano /etc/auto_master

#
# Automounter master map
#
+auto_master		# Use directory service
/net			-hosts		-nobrowse,hidefromfinder,nosuid
/home			auto_home	-nobrowse,hidefromfinder
/Network/Servers	-fstab
/-			-static
/Users/potomushto/nas		auto_db
```

And **the second is newly created `auto_db`:**:

```
sudo nano /etc/auto_nas

db -fstype=smbfs ://login:password@MyCloudEX2Ultra/Public/db
```

Finally, run `sudo automount -vc` and got it worked.
<commit_msg>Fix actual error|<commit_after>---
layout: post
title: "Setting up Postgres database's location on NAS"
excerpt: On MacOS using Autofs
tags: [postgres, macos, nas, wd]
comments: true
---

Network storages are really handy. Much more disk space,
RAID-0, fast and reliable shared storage across family. They have decent small CPUs on the board, you can manage the time table when storage is enabled and have mobile apps. I wish I had NAS before.

I bought one from Western Digital (MyCloud EX2Ultra to be specific), and
one of the main reason for that is being able to work with a huge database.
In my current case, it's Postgres, which runs on my Mac with 256GB, so any database larger than XX Gb is large enough. The cloud solution is also possible solution. But what about 2TB database?

My first attempt was about mount drive manually and create the database there:

```
mount -t smbfs smb://MyCloudEX2Ultra/Public/db db
initdb --debug -D nas/db/postgres
```

And it failed. It couldn't make symlinks on remote drive:

```
could not link file "pg_xlog/xlogtemp.71138" to "pg_xlog/000000010000000000000001": Operation not supported
```

After rebooting in safe mode (Cmd+R) I run:

```
csrutil disable
reboot
```

That needed because by default Mac has System Integrity Protection and it won't allow editing system files. You can disable that:

```
sudo nano /System/Library/LaunchDaemons/com.apple.smbd.plist
<key>ProgramArguments</key>
<array>
     <string>/usr/sbin/smbd</string>
     <string>--no-symlinks</string>
     <string>false</string>
</array>
```

It didn't help either.

I choose easier way, just store logs locally.

```
initdb --debug -D nas/db/postgres -X db_wal
```

And that worked. Now I could run postgres like
```
pg_ctl -D ~/nas/db/postgres start &
```

### Auto mounting

After successfully creating db on the remote shared disk all I needed is setting up automounting directory on the system startup using `autofs`.

It requires editing two files. **First, is /etc/auto_master**

```
sudo nano /etc/auto_master

#
# Automounter master map
#
+auto_master		# Use directory service
/net			-hosts		-nobrowse,hidefromfinder,nosuid
/home			auto_home	-nobrowse,hidefromfinder
/Network/Servers	-fstab
/-			-static
/Users/potomushto/nas		auto_nas
```

And **the second is newly created `auto_db`:**:

```
sudo nano /etc/auto_nas

db -fstype=smbfs ://login:password@MyCloudEX2Ultra/Public/db
```

Finally, run `sudo automount -vc` and got it worked.

{'fraction_non_alphanumeric': 0.0960016237061092, 'fraction_numerical': 0.015019281510046681, 'mean_word_length': 4.106735751295337, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1357151', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1522, 'n_words': 644}