466

I use postgres from homebrew in my OS X, but when I reboot my system, sometimes the postgres doesn't start after the reboot, and so I manually tried to start it with postgres -D /usr/local/var/postgres, but then the error occurred with the following message: FATAL: could not open directory "pg_tblspc": No such file or directory.

The last time it occurred, I couldn't get it to the original state, so I decided to uninstall the whole postgres system and then re-installed it and created users, tables, datasets, etc... It was so disgusting, but it frequently occurs on my system, say once in a few months.

So why does it lose the pg_tblspc file frequently? And is there anything that I can do to avoid the loss of the file?

I haven't upgraded my homebrew and postgres to the latest version (i.e. I've been using the same version). Also, all the things that I did on the postgres database is delete the table and populate the new data every day. I haven't changed the user, password, etc...

EDIT (mbannert): I felt the need to add this, since the thread is the top hit on google for this issue and for many the symptom is different. Homebrewers likely will encounter this error message:

No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

So, if you just experienced this after the Yosemite upgrade you now you're covered for now reading this thread.

Donovan
  • 15,591
  • 4
  • 19
  • 33
Blaszard
  • 27,599
  • 40
  • 143
  • 217
  • Eep, it really, really shouldn't! When you say "latest version", please show the exact version number. Also, have you put any tablespaces on external storage? where's the PostgreSQL data directory located? – Craig Ringer Sep 22 '14 at 09:01
  • Also, `pg_tblspc` is a *directory*. The only way I can see this directory and just this directory randomly vanishing is filesystem corruption or a particularly badly behaved virus scanner or file sync tool. – Craig Ringer Sep 22 '14 at 11:53
  • I don't have any virus scanner. I don't know what `tablespaces` is, so I don't think I put it on external storage. – Blaszard Sep 22 '14 at 12:28
  • Hm. All I can tell you is that something's *badly* wrong. `pg_tblspc` does not just disappear on any system I've ever encountered, nor can I imagine a sane reason it would. It's going to be very hard to say what makes your system different without a lot more detail. – Craig Ringer Sep 22 '14 at 12:29
  • What I've done on my postgres is 1) select one database, 2) delete the entire data on the DB, 3) populate the new data to the DB, 4) push it to heroku. This procedure is done every day. Do you think that pushing it to heroku DB is something related? – Blaszard Sep 22 '14 at 12:33
  • "select one database", "delete the entire data on the db". Details? – Craig Ringer Sep 22 '14 at 12:35
  • Ah, sorry that was wrong. I did all the procedure above on my MySQL system, and then copy the mysql database to the corresponding postgres database using `py-mysql2pgsql`(https://github.com/philipsoutham/py-mysql2pgsql) – Blaszard Sep 22 '14 at 12:38
  • OK, well, that seems unlikely to be related, it's just using PostgreSQL's client libraries or generating an SQL script. You'd have to mess directly with the data directory to create the problem you're encountering. – Craig Ringer Sep 22 '14 at 12:41
  • Then is the OS upgrade relevant? I use Yosemite beta right now, but I'm not sure when it occurred previously. I've updated it whenever the new beta is available (totally 6 or 7 times I think). – Blaszard Sep 22 '14 at 12:50
  • Beta operating system? Yeah, that's probably a candidate for the issue. Perhaps you should raise this with Apple, you might've found a bug. Again, nearly impossible to say without detailed investigation and the ability to actually reproduce the problem. – Craig Ringer Sep 22 '14 at 12:51
  • 2
    Were you able to find a solution for this @Gardecolo? I'm having the same issue after upgrading to Yosemite. – Donovan Sep 23 '14 at 17:26
  • @Donovan, no. I've uninstalled and re-installed it. I do never think I should do, but I had to recover it as soon as possible because I use it for my business... – Blaszard Sep 23 '14 at 17:50
  • @CraigRinger Don't know about the beta testing at Apple inc., but the release version of Yosemite still has the issue. Enfin, came here and problem solved. – Roy Prins Nov 06 '14 at 20:50
  • I wrote a blog entry following up on this and another issue after researching the issue some more. Overall I simply cannot believe how Apple has handled this. http://blog.2ndquadrant.com/ware-yosemite-possible-postgresql-upgrade-issues-os-x-10-10/ – Craig Ringer Nov 07 '14 at 01:46
  • Has anyone managed to find out what in OS X is doing this yet? It's crazy behaviour. – Craig Ringer Oct 07 '15 at 08:35
  • I have a custom installation path under $HOME, one day I thought I would clean up all empty directories and this happened during the next PostgresSQL restart. Empty directories are not useless after all. – RajaRaviVarma Dec 22 '17 at 09:37

6 Answers6

933

Solved... in part.

Apparently, Installing the latest versions of OS X (e.g. Yosemite or El Capitan) removes some directories in /usr/local/var/postgres.

To fix this you simply recreate the missing directories:

mkdir -p /usr/local/var/postgres/pg_commit_ts
mkdir -p /usr/local/var/postgres/pg_dynshmem
mkdir -p /usr/local/var/postgres/pg_logical/mappings
mkdir -p /usr/local/var/postgres/pg_logical/snapshots
mkdir -p /usr/local/var/postgres/pg_replslot
mkdir -p /usr/local/var/postgres/pg_serial
mkdir -p /usr/local/var/postgres/pg_snapshots
mkdir -p /usr/local/var/postgres/pg_stat
mkdir -p /usr/local/var/postgres/pg_stat_tmp
mkdir -p /usr/local/var/postgres/pg_tblspc
mkdir -p /usr/local/var/postgres/pg_twophase

Or, more concisely (thanks to Nate):

mkdir -p /usr/local/var/postgres/{{pg_commit_ts,pg_dynshmem,pg_replslot,pg_serial,pg_snapshots,pg_stat,pg_stat_tmp,pg_tblspc,pg_twophase},pg_logical/{mappings,snapshots}}

Rerunning pg_ctl start -D /usr/local/var/postgres now starts the server normally and, at least for me, without any data loss.

UPDATE

On my system, some of those directories are empty even when Postgres is running. Maybe, as part of some "cleaning" operation, Yosemite removes any empty directories? In any case, I went ahead and created a '.keep' file in each directory to prevent future deletion.

touch /usr/local/var/postgres/{{pg_commit_ts,pg_dynshmem,pg_replslot,pg_serial,pg_snapshots,pg_stat,pg_stat_tmp,pg_tblspc,pg_twophase},pg_logical/{mappings,snapshots}}/.keep

Note: Creating the .keep file in those directories will create some noise in your logfile, but doesn't appear to negatively affect anything else.

huyz
  • 2,157
  • 1
  • 24
  • 30
Donovan
  • 15,591
  • 4
  • 19
  • 33
  • 1
    Do you still lose other files/directories in `/usr/local/var/postgres`, such as `pg_clog`, `pg_stat`, `pg_subtrans`, `base`, etc...? – Blaszard Sep 23 '14 at 17:58
  • 1
    I did not, no. I was only missing those three – Donovan Sep 23 '14 at 17:59
  • In my case all the files that remained there were `postmaster.opts` and `server.log`. But thank you for the information. – Blaszard Sep 23 '14 at 18:00
  • May be due to removing and re-installing Postgres? – Donovan Sep 23 '14 at 18:02
  • 1
    After re-installed postgres, everything looks fine. So I think it's due to the installation of Yosemite Beta. – Blaszard Sep 23 '14 at 18:03
  • I agree. In my case it was only those three directories, but since I don't know why it's removing *any* directories, it's entirely possible it could remove others as well. – Donovan Sep 23 '14 at 18:06
  • I had the exact same issue—this fixed it. Installation of GM Candidate (14A379a) over existing (and working) 10.9.5. The Yosemite installer must have removed these empty directories. – thomasfuchs Oct 03 '14 at 01:51
  • Reinstalling Postgres with Brew didn't recreate those directories. So, I had to follow Donovan's advice and create them by hand. – Vasily Oct 17 '14 at 02:04
  • 54
    Just a suggestion for a more concise command: `mkdir -p /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/` and `touch /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/.keep` – Nate Oct 17 '14 at 13:45
  • And this fixes PostG after installing Yosemite 10.10 release. – Karl Oct 17 '14 at 13:55
  • Thanks! Worked for me on latest build of Yosemite and postgres 9.3.2 on – Lenart Oct 17 '14 at 14:41
  • Thanks! Had the same problem after upgrading to Yosemite. Brew upgrade postgres didn't help. – guapolo Oct 17 '14 at 17:18
  • 2
    You may not need to create all three directories (I only had to create two). Run `pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start -w` to get better output when starting postgres. Then do `tail -5 /usr/local/var/postgres/server.log` to see errors on startup. The errors will indicate which directories are missing. – Richard Jones Oct 17 '14 at 17:19
  • 26
    Those .keep files actually cause me some grief in the server logs: `could not open temporary-files directory "pg_tblspc/.keep/PG_9.3_201306121/pgsql_tmp": Not a directory` – funwhilelost Oct 20 '14 at 20:19
  • 1
    @infamouse Yes, I get those log entries too, but they don't trouble me. If they bother you, you can remove the .keep files as are not strictly necessary. They simply keep the directories from being accidentally deleted (since they're not empty). – Donovan Oct 20 '14 at 20:23
  • 13
    I was also missing pg_snapshots and pg_stat directories. – Jon Stevens Oct 20 '14 at 21:21
  • @infamouse if the logs are annoying try chown-ing the .keep files and chmod-ing them so that the user that postgres runs as cannot see the files ( Note this might be impossible ) – MichaelStoner Oct 23 '14 at 16:43
  • 1
    Thank you for this...I just upgraded to Yosemite this morning and, right on cue, PostgreSQL stopped working because apparently Apple just has to eff with non-OS-essential packages. I'm getting tired of every OS X upgrade being such a fiasco. – Kyle K Oct 25 '14 at 17:51
  • If you watch the Yosemite installer (Command-L while it is running and taking FOREVER!!) you will see it moving everything in /usr/local around furiously - no idea why, but I bet that empty directories get lost. How convenient! – Ben Walding Oct 26 '14 at 03:17
  • 1
    In my case the .keep files should be directories, so I just executed this: `mkdir -p /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/.keep` – David Morales Oct 27 '14 at 16:11
  • to start postgres i need to ran `pg_ctl start -D /usr/local/var/postgres/*` – moohkooh Oct 29 '14 at 07:32
  • 8
    I also had to create one extra directory 'pg_replslot'. Except that it works fine. Thanks! – Lucas Jan 06 '15 at 16:52
  • 6
    experienced same as @Lucas for bottled postgres 9.4.0. I had to `mkdir /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp,pg_replslot}` – Patrick Farrell Feb 03 '15 at 19:21
  • 3
    Experienced this, but had to run the following to get a server.log without warnings or errors: `mkdir -p /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp,pg_replslot,pg_stat,pg_logical/snapshots,pg_logical/mappings,pg_snapshots}/`. This is for brew postgresql 9.4.1 – Colin Curtin Apr 25 '15 at 22:34
  • How do you get Postgres to automatically start when the system is rebooted? It seems that everytime I restart my computer now after implementing your solution I need to run pg_ctl start -D /usr/local/var/postgres again to get everything working and I didn't need to do this before the upgrade to Yosemite. – heading_to_tahiti May 12 '15 at 22:33
  • 1
    Had to do the same after installing the latest version of postgres 9.4.5_2 on El Capitan. Your solution is still applicable. Works. – A. J. Alger Jan 07 '16 at 16:05
  • 3
    I had to add `/usr/local/var/postgres/pg_logical/snapshots` and `/usr/local/var/postgres/pg_logical/mappings` to start the server. (Yosimite, brew install homebrew/versions/postgresql94) – f1lt3r Jan 21 '16 at 04:09
9

Donavan's answer is spot on, I just wanted to add that as i did different things with the database (e.g. rake db:test), it went looking for different directories that haven't been mentioned above and would choke when they weren't present, in my case pg_logical/mappings, so you may want to setup a terminal running:

tail -f /usr/local/var/postgres/server.log

and watch it for missing folders while you go thru your typical database activities.

Community
  • 1
  • 1
tony_k
  • 1,777
  • 2
  • 19
  • 25
6

This is slightly off-topic but worth noting here as part of the PostgreSQL Yosemite recovery process. I had the same issue as above AND I had an issue with PostgreSQL "seemingly" running in the background so even after adding directories I couldn't restart. I tried using pg_ctl stop -m fast to kill the PostgreSQL server but no luck. I also tried going after the process directly with kill PID but as soon as I did that a PostgreSQL process re-appeared with a different PID.

The key ended up being a .plist file that Homebrew had loaded... The fix for me ended up being:

launchctl unload /Users/me/Library/LaunchAgents/homebrew.mxcl.postgresql92.plist

After that I was able to start PostgreSQL normally.

techraf
  • 53,268
  • 22
  • 149
  • 166
MCP
  • 4,016
  • 11
  • 38
  • 52
  • My plist was named slightly differently: `launchctl unload ${HOME}/Library/LaunchAgents/homebrew.mxcl.postgresql.plist` but basically that was also the same problem for me, and the same solution. – onekiloparsec Nov 03 '14 at 15:43
4

The missing directories need to be present in your PostgreSQL data directory. The default data directory is /usr/local/var/postgres/. If you have set up a different data directory, you need to re-create the missing directories there. If you modified the homebrew-recommended .plist file that starts PostgreSQL, you can find the data directory there:

cat ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

(it's the -D option you started postgres with:)

  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/postgres</string>
    <string>-D</string>
    <string>/usr/local/pgsql/data</string>

In the example above, you'd create the missing directories in /usr/local/pgsql/data, like so:

cd /usr/local/pgsql/data
mkdir {pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots,pg_logical}
mkdir pg_logical/{snapshots,mappings}
techraf
  • 53,268
  • 22
  • 149
  • 166
tee
  • 3,629
  • 1
  • 27
  • 44
0

I was having this issue with a dockerized Rails application.

Instead of pg_tblspc and other directories missing from /usr/local/var/postgres, they were missing from myRailsApp/tmp/db.

You will want to use a similar version of Donovan's solution, you will just have to alter to have the correct path for your Rails app...

mkdir /myRailsApp/tmp/db/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/

Also, you will want to add a .keep file to make sure git doesn't disregard the empty directories.

touch /myRailsApp/tmp/db/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/.keep

I noticed an error with a .keep in 1 of the directories, so just read the command line output carefully and adjust as needed.

BTB
  • 62
  • 6
-20

Creating the missing directories certainly works but I fixed it by reinititializing postgres db, this is a cleaner approach to avoid future problems.

NOTE: This approach will delete existing databases

$ rm -r /usr/local/var/postgres
$ initdb -D /usr/local/var/postgres
Greg
  • 4,261
  • 2
  • 27
  • 22
  • 19
    Obviously deleting existing databases is not a minor exception here. This a bit like saying "I couldn't find /var/tmp so I reinstalled the operating system." – Adam Donahue Oct 20 '14 at 19:44
  • 4
    Oh, man, this is "cleaner" than anything I can think of :) Just hope some random copy-paster from the interwebz doesn't shoot this directly into their console without looking at it :) – Halil Özgür Oct 25 '14 at 13:31
  • 2
    Sorry for the down vote Greg but I recommend rewording your solution to make it explicit that this approach should only be used in development or if the user can afford to wipe their DB. – hraynaud Nov 07 '14 at 11:54
  • 1
    Why is this downvoted so much? On a dev server this is the right way. – Jordon Bedwell Aug 10 '18 at 13:59
  • @JordonBedwell even on a dev server is bad idea, unless you are playing with a single app using db on your computer.. It's like "I can't start my favorite code editor, let's reinstall the OS" – Andre Figueiredo Oct 02 '19 at 20:52