How to upgrade PostgreSQL from version 9.6 to version 10.1 without losing data?

后端 未结 15 2577
挽巷
挽巷 2020-12-02 03:33

I\'m using the PostgreSQL database for my Ruby on Rails application (on Mac OS X 10.9).

Are there any detailed instructions on how to upgrade PostgreSQL database?

相关标签:
15条回答
  • 2020-12-02 04:14

    Assuming you've used home-brew to install and upgrade Postgres, you can perform the following steps.

    1. Stop current Postgres server:

      launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

    2. Initialize a new 10.1 database:

      initdb /usr/local/var/postgres10.1 -E utf8

    3. run pg_upgrade (note: change bin version if you're upgrading from something other than below):

      pg_upgrade -v \
          -d /usr/local/var/postgres \
          -D /usr/local/var/postgres10.1 \
          -b /usr/local/Cellar/postgresql/9.6.5/bin/ \
          -B /usr/local/Cellar/postgresql/10.1/bin/
      

      -v to enable verbose internal logging

      -d the old database cluster configuration directory

      -D the new database cluster configuration directory

      -b the old PostgreSQL executable directory

      -B the new PostgreSQL executable directory

    4. Move new data into place:

      cd /usr/local/var
      mv postgres postgres9.6
      mv postgres10.1 postgres
      
    5. Restart Postgres:

      launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

    6. Check /usr/local/var/postgres/server.log for details and to make sure the new server started properly.

    7. Finally, re-install the rails pg gem

      gem uninstall pg
      gem install pg
      

    I suggest you take some time to read the PostgreSQL documentation to understand exactly what you're doing in the above steps to minimize frustrations.

    0 讨论(0)
  • 2020-12-02 04:14

    My solution for upgrading from Postgresql 11 to Postgresql 12 on Windows 10 is the following.

    As a first remark you will need to be able stop and start the Postgresql service. You can do this by the following commands in Powershell.

    Start: pg_ctl start -D “d:\postgresql\11\data”

    Stop: pg_ctl stop -D “d:\postgresql\11\data”

    Status: pg_ctl status -D “d:\postgresql\11\data”

    It would be wise to make a backup before doing the upgrade. The Postgresql 11 instance must be running. Then to copy the globals do

    pg_dumpall -U postgres -g -f d:\bakup\postgresql\11\globals.sql

    and then for each database

    pg_dump -U postgres -Fc <database> > d:\backup\postgresql\11\<database>.fc

    or

    pg_dump -U postgres -Fc -d <database> -f d:\backup\postgresql\11\<database>.fc

    If not already done install Postgresql 12 (as Postgresql 11 is also installed this will be on port 5433)

    Then to do the upgrade as follows:

    1) Stop Postgresql 11 service (see above)

    2) Edit the postgresql.conf file in d:\postgresql\12\data and change port = 5433 to port = 5432

    3) Edit the windows user environment path (windows start then type env) to point to Postgresql 12 instead of Postresql 11

    4) Run upgrade by entering the following command.

    pg_upgrade `
    -b “c:\program files\postgresql\11\bin” `
    -B “c:\program files\postgresql\12\bin” `
    -d “d:\postgresql\11\data” `
    -D “d:\postgresql\12\data” --username=postgres
    

    (In powershell use backtick (or backquote) ` to continue the command on the next line)

    5) and finally start the new Postgresql 12 service

    pg_ctl start -D “d:\postgresql\12\data”

    0 讨论(0)
  • 2020-12-02 04:15

    Looks like the solution has been baked into Homebrew now:

    $ brew info postgresql
    ...
    ==> Caveats
    To migrate existing data from a previous major version of PostgreSQL run:
      brew postgresql-upgrade-database
    ....
    
    0 讨论(0)
提交回复
热议问题