Skip to content

Instantly share code, notes, and snippets.

@ptmt
Created December 15, 2018 11:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptmt/40e566f7b0c5dc2ffd0ff59466ab6452 to your computer and use it in GitHub Desktop.
Save ptmt/40e566f7b0c5dc2ffd0ff59466ab6452 to your computer and use it in GitHub Desktop.
Setting up Postgres on NAS

Network storages are really handy when it comes to a lot of disk space. Cheaper disk space available, RAID and after all it's a small server, 1GHz/256MB in my case. I wish I had NAS before.

I bought the 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. Consider Postgres, which runs on my Mac with 256GB, so any database larger than XX Gb is large enough to worry about. You can solve this problem with cloud, but what about 2TB database? 20TB?

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

It didn't work. One couldn't make symlinks on remote drive by default:

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

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

csrutil disable
reboot

It's needed because by default Mac has System Integrity Protection and it won't allow editing system files. I was hoping to disable that temporary and allow symlinks:

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>

After doing all of that I still had some errors with symlinks while creating Postgres database.

I choose an easier way: storing the append log locally.

initdb -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.

Now I always got nas folder mounted automatically.

@fachryansyah
Copy link

how about performance ? when many user trying to write / queries data ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment