0

My biggest problem is that every time i turn off my laptop i lose all my data. I just started learning rails with postgres.

So how can i start and stop properly a postgres database on the terminal.

I'm on Mac OS X.

this is what i use to start it:

postgres -D /usr/local/var/postgres/data 

and this to stop it:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8 but i think what it does it removes it so i lose all my data 
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
Jayswager
  • 211
  • 1
  • 2
  • 12
  • Using the `pg_ctl` comand? `pg_ctl stop`. – jordanm Sep 17 '15 at 02:30
  • postgres -D /usr/local/var/postgres/data this is what i use to start it and this to stop it rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8 but i think what it does it removes it so i lose all my data – Jayswager Sep 17 '15 at 02:40
  • 3
    Yes, `rm` deletes data. – jordanm Sep 17 '15 at 02:51
  • 3
    Never run a command without knowing what it does. – brettwhiteman Sep 17 '15 at 03:35
  • how is your Postgres server _started_? If that is registered as a service in the system, a clean shut down of the operating system should also cleanly shut down Postgres - at least this happens on Windows and Linux. As the MacOS is a "Unix-Style" OS at heart it should work there as well (I have never used a Mac). How did you install Postgres? How do you turn off your Mac? –  Sep 17 '15 at 05:49
  • Augh. Yes, you are saying "Delete all my data, then create a new blank PostgreSQL install". That is why you have a new blank PostgreSQL install when you run it. – Craig Ringer Sep 18 '15 at 04:59

2 Answers2

1

Short Answer (If you don't have time):

pg_ctl stop -m immediate

That should do an instant shutdown, with an implied auto-recovery (no data loss). But this is not always recommended, read below for why.

Long Answer: 2 Things need to be taken note of: 1. You should lookup here. The -m option allows three modes. Depending on whether this is a heavy production box or your local system you need to chose between the three options (fast / smart / immediate). The implications of all three are given if you search for the word 'Immediate' on this page. 2. Read the Description section nonetheless, regarding the Modes to gain more clarity about why there is no one solution for all problems.

Robins Tharakan
  • 2,209
  • 19
  • 17
1

You are deleting your PostgreSQL install then creating a new one.

Your commands:

rm -rf /usr/local/var/postgres &&
initdb /usr/local/var/postgres -E utf8 &&
postgres -D /usr/local/var/postgres/data

do:

  • Delete PostgreSQL data directory, removing all data
  • Create a new blank PostgreSQL instance
  • Start the PostgreSQL instance

Clearly this is not what you want.

You should instead use the documented method of starting and stopping your particular PostgreSQL install. Since you're using /usr/local/var/postgres I'm guessing you're using Homebrew to install PostgreSQL, so see this answer. Basically, install a launchd plist file and use launchd.

Don't run commands you don't understand, and read the documentation; you'll have fewer of this kind of problem.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778