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

后端 未结 15 2576
挽巷
挽巷 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 03:52

    Standing on the shoulders of the other poor creatures trodding through this muck, I was able to follow these steps to get back up and running after an upgrade to Yosemite:

    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 9.4 database:

      initdb /usr/local/var/postgres9.4 -E utf8

    3. Install postgres 9.3 (as it was no longer present on my machine):

      brew install homebrew/versions/postgresql93

    4. Add directories removed during Yosemite upgrade:

      mkdir -p /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/touch /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/.keep

    5. run pg_upgrade:

      pg_upgrade -v -d /usr/local/var/postgres -D /usr/local/var/postgres9.4 -b /usr/local/Cellar/postgresql93/9.3.5/bin/ -B /usr/local/Cellar/postgresql/9.4.0/bin/

    6. Move new data into place:

      cd /usr/local/var
      mv postgres postgres9.3
      mv postgres9.4 postgres
      
    7. Restart Postgres:

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

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

    9. Finally, re-install related libraries?

      pip install --upgrade psycopg2
      gem uninstall pg
      gem install pg
      
    0 讨论(0)
  • 2020-12-02 03:53

    Here is the solution for Ubuntu users

    First we have to stop postgresql

    sudo /etc/init.d/postgresql stop
    

    Create a new file called /etc/apt/sources.list.d/pgdg.list and add below line

    deb http://apt.postgresql.org/pub/repos/apt/ utopic-pgdg main
    

    Follow below commands

    wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
    sudo apt-get update
    sudo apt-get install postgresql-9.4
    sudo pg_dropcluster --stop 9.4 main 
    sudo /etc/init.d/postgresql start
    

    Now we have everything, just need to upgrade it as below

    sudo pg_upgradecluster 9.3 main
    sudo pg_dropcluster 9.3 main
    

    That's it. Mostly upgraded cluster will run on port number 5433. Check it with below command

    sudo pg_lsclusters
    
    0 讨论(0)
  • 2020-12-02 03:55

    On Windows I kept facing different errors messages when trying to use pg_upgrade.

    Saved a lot of time for me to just:

    1. Backup DB
    2. Uninstall all copies of PostgreSQL
    3. Install 9.5
    4. Restore DB
    0 讨论(0)
  • 2020-12-02 03:56

    Despite all answers above, here goes my 5 cents.

    It works on any OS and from any-to-any postgres version.

    • Stop any running postgres instance;
    • Install the new version and start it; Check if you can connect to the new version as well;
    • Change old version's postgresql.conf -> port from 5432 to 5433;
    • Start the old version postgres instance;
    • Open a terminal and cd to the new version bin folder;
    • Run pg_dumpall -p 5433 -U <username> | psql -p 5432 -U <username>
    • Stop old postgres running instance;
    0 讨论(0)
  • 2020-12-02 03:56

    My solution was to do a combination of these two resources:

    https://gist.github.com/tamoyal/2ea1fcdf99c819b4e07d

    and

    http://www.gab.lc/articles/migration_postgresql_9-3_to_9-4

    The second one helped more then the first one. Also to not, don't follow the steps as is as some are not necessary. Also, if you are not being able to backup the data via postgres console, you can use alternative approach, and backup it with pgAdmin 3 or some other program, like I did in my case.

    Also, the link: https://help.ubuntu.com/stable/serverguide/postgresql.html Helped to set the encrypted password and set md5 for authenticating the postgres user.

    After all is done, to check the postgres server version run in terminal:

    sudo -u postgres psql postgres
    

    After entering the password run in postgres terminal:

    SHOW SERVER_VERSION;
    

    It will output something like:

     server_version 
    ----------------
     9.4.5
    

    For setting and starting postgres I have used command:

    > sudo bash # root
    > su postgres # postgres
    
    > /etc/init.d/postgresql start
    > /etc/init.d/postgresql stop
    

    And then for restoring database from a file:

    > psql -f /home/ubuntu_username/Backup_93.sql postgres
    

    Or if doesn't work try with this one:

    > pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d name_of_database ~/your_file.dump
    

    And if you are using Rails do a bundle exec rake db:migrate after pulling the code :)

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

    On Windows 10 since I had npm, I installed rimraf package. npm install rimraf -g

    Backup all your databases one by one using command pg_dump -U $username --format=c --file=$mydatabase.sqlc $dbname

    Then Installed Latest PostgreSQL Version i.e. 11.2 which prompted me to use port 5433 this time.

    Followed by Uninstall of older versions of PostgreSQL mine was 10. Note the uninstaller may give a warning of not deleting folder C:\PostgreSQL\10\data. That's why we have the next step using rimraf to permanently delete the folder and it's sub-folders.

    change into PostgreSQL install directory and ran the command rimraf 10. 10 is a directory name. Note use your older version of PostgreSQL i.e. 9.5 or something.

    Now add C:\PostgreSQL\pg11\bin, C:\PostgreSQL\pg11\lib into the Windows environmental variables. Note my new installed version is 11 thus why I am using pg11.

    Navigate to C:\PostgreSQL\data\pg11 then open postgresql.conf edit port = 5433 to port = 5432

    That's it. Open cmd and type psql -U postgres

    You can now restore all your backed databases one by one using the command pg_restore -U $username --dbname=$databasename $filename

    0 讨论(0)
提交回复
热议问题