Artifactory upgrade fail, postgres 9.5 -> 9.6 upgrade instructions needed - upgrade

I had planned an upgrade of artifactory from 6.7.5 to 6.8.1. As part of the upgrade I checked jfrog's repo on github and it looks like they have a new recommended nginx and postgres version.
The current docker-compose is using postgres 9.5 and the new default version if 9.6. Simply pulling down postgres 9.6 however does not do an inplace upgrade.
FATAL: database files are incompatible with server DETAIL: The data
directory was initialized by PostgreSQL version 9.5, which is not
compatible with this version 9.6.11.
The upgrade instructions do not mention anything about how to do the upgrade.

The examples provided in github (https://github.com/jfrog/artifactory-docker-examples) are just examples.
Using them in production could cause issues and backwards compatibility is not guaranteed.
To get over the PostgreSQL matter when upgrading, I would suggest:
$ docker-compose -f yml-file-name.yml stop
edit the yml-file-name.yml and change the docker.bintray.io/postgres:9.6.11 to docker.bintray.io/postgres:9.5.2
$ docker-compose -f yml-file-name.yml up -d
Artifactory should be upgraded after following this, however it will keep using the previous version of the PostgreSQL DB

I have been able to upgrade database using following approach:
Dump all database to an SQL script using old database image; store it in a volume for future import:
# Override PostgreSQL image used to export using old binaries
printf "version: '2.1'\nservices:\n postgresql:\n image: docker.bintray.io/postgres:9.5.2\n" > image_override.yml
started_container=$(docker-compose -f artifactory-pro.yml -f image_override.yml run -d -v sql_dump_volume:/tmp/dump --no-deps postgresql)
# Dump database to a text file in a volume (to make it available for import)
docker exec "${started_container}" bash -c "until pg_isready -q; do sleep 1; done"
docker exec "${started_container}" bash -c "pg_dumpall --clean --if-exists --username=\${POSTGRES_USER} > /tmp/dump/dump.sql"
docker stop "${started_container}"
docker rm --force "${started_container}"
Back up old database directory and prepare a new one:
mv -fv /data/postgresql /data/postgresql.old
mkdir -p /data/postgresql
chown --reference=/data/postgresql.old /data/postgresql
chmod --reference=/data/postgresql.old /data/postgresql
Run a new database image with mounting dump script from step 1. It processes SQL scripts upon startup when setting up a new database, provided it's started as postgres something. We just don't need to leave the server running afterwards, so I provided --version to make entrypoint execute, import the data and quit:
docker-compose -f artifactory-pro.yml run --rm --no-deps -e POSTGRES_DB=postgres -e POSTGRES_USER=root -v sql_dump_volume:/docker-entrypoint-initdb.d postgresql postgres --version
After all this is done, I was able to start Artifactory normally with docker-compose -f artifactory-pro.yml up -d and it started up normally, applying rest of schema and file upgrade procedure as usual.
I have also prepared a script that basically does the above steps along with some additional checks and cleanup. Feel free to use if you find it useful.

Related

data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.0

I have downloaded Portainer onto my server and created a PostgreSQL database in a container there.
Today I could no longer get access to the database. The log shows a message that there is a version problem.
I already read into some similar issues like Postgres container crashes with `database files are incompatible with server` after container's image has been updated to the latest one and Postgres container crashes with `database files are incompatible with server` after container's image has been updated to the latest one
and the solutions brew postgresql-upgrade-database did not work.
What can I do?
LOG
2021-10-03 [1] FATAL: database files are incompatible with server
2021-10-03 [1] DETAIL: The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.0 (Debian 14.0-1.pgdg110+1).
PostgreSQL Database directory appears to contain a database; Skipping initialization
I also found this https://www.postgresql.org/docs/14/upgrading.html but the commands didn't work. Do I need to do this in the container somehow, or what commands will work to keep it running in the container?
You need to update the data file to the new format with this command:
$ brew postgresql-upgrade-database
I resolved it by
Removing the postgres image
Remove the volume
Pull the image again
Assuming you know the docker commands for above step.
On top of the answer suggesting removing all volumes/images/containers - if you have a shared volume for the DB in docker-compose.yml, like:
db:
image: postgres:14.1-alpine
volumes:
- ./tmp/db:/var/lib/postgresql/data
You will also need to remove the postgres mapped volume data which lives in ./tmp/db. Otherwise those conflicting files would still be there.
If you don't care about the data - you use the database for development purposes and you'll be able to re-create the db easily, just run rm -r ./tmp/db. Than you can just docker-compose up and re-create your database.
In case you care about the data, use pg_dumpall to dump you data before removing the files and restore after you run docker-compose up and your postgres service is ready again.
had a problem after running
brew update
brew upgrade
Brew upgraded postgresql to 14 which gave me
psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have also seen the same error you have by inspecting
tail /usr/local/var/postgres/server.log
I have downgraded to version 13 running
brew uninstall postgresql
brew install postgresql#13
echo 'export PATH="/usr/local/opt/postgresql#13/bin:$PATH"' >> ~/.zshrc
and the command I've used to start the DB again is
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
I think because of the difference postgresql13 with 14 in initialization.
You can make a backup of your database before the new version, delete it and migrate database to the new version
for example in django:
dump: ./manage.py dumpdata -o mydata.json
load: ./manage.py loaddata mydata.json
django: Of course, if you have important information, do this separately for each model and pay attention to the dependencies when loading.
In docker-compose.yml you could change
db-data:/var/lib/postgresql/data:rw
to this
db-data:/var/lib/postgresql#14/data:rw
Delete image and run
docker-compose up -d
one more time
I was also facing the same issue with postgres in keycloak.
Downgrading the version to 13 resolved my issue.
for me the database directory was in /tmp/db and since no important data was there I remove all and everything worked well ...
but if your data is important then read these:
https://www.postgresql.org/docs/13/upgrading.html
https://www.postgresql.org/docs/13/pgupgrade.html
https://www.kostolansky.sk/posts/upgrading-to-postgresql-14/
I resolved it by
Removing the postgres container
docker rm "container name"
Removing the postgres image
docker rmi "image name"
docker-compose stop
docker-compose down
list all volumes
docker volume ls
remove volume
docker volume rm "volume name"
Pull the image again
Assuming you know the docker commands for above step.
I had the same issue after home brew update on my mac - Install #14 of postgresql
This is what help to me:
Install old binaries v13
brew install postgresql#13
Backup Your old DB folder
Initdb with new name
now use pg_upgrade like this:
pg_upgrade --old-datadir [Your old DB folder] --new-datadir [Your new
DB folder] --old-bindir [link to old bin (v13)]
for example:
pg_upgrade --old-datadir PSQ-data --new-datadir PSQ-data_new
--old-bindir /usr/local/Cellar/postgresql#13/13.9/bin
Source for old bin You will have after install #13, just look what you receive on terminal after install "Summary".
After this operation just start sever with new DB

How to upgrade the pg_restore in docker postgres image 10.3 to 10.5

I use tableplus for my general admin.
Currently using the docker postgres image at 10.3 for both production and localhost development.
Because tableplus upgraded their postgres 10 drivers to 10.5, I can no longer use pg_restore to restore the backup files which are dumped using 10.5 --format=custom
See image for how I backup using tableplus. And how it uses 10.5 driver
The error message I get is pg_restore: [archiver] unsupported version (1.14) in file header
What i tried
I tried in localhost to simply change the tag for postgres in my dockerfile from 10.3 to 10.5 and it didn't work
original dockerfile
FROM postgres:10.3
COPY ./maintenance /usr/local/bin/maintenance
RUN chmod +x /usr/local/bin/maintenance/*
RUN mv /usr/local/bin/maintenance/* /usr/local/bin \
&& rmdir /usr/local/bin/maintenance
to
FROM postgres:10.5
COPY ./maintenance /usr/local/bin/maintenance
RUN chmod +x /usr/local/bin/maintenance/*
RUN mv /usr/local/bin/maintenance/* /usr/local/bin \
&& rmdir /usr/local/bin/maintenance
My host system for development is macOS.
I have many existing databases and schemas in my development docker postgres. So I am currently stumped as to how to upgrade safely without destroying old data.
Can advise?
Also I think a long term is to figure out how to have data files outside the docker (i.e. inside my host system) so that everytime I want to upgrade my docker image for postgres I can do so safely without fear.
I like to ask about how to switch to such a setup as well.
If I understand you correctly, you want to restore a custom format dump taken with 10.5 into a 10.3 database.
That won't be possible if the archive format has changed between 10.3 and 10.5.
As a workaround, you could use a “plain format” dump (option --format=plain) which does not have an “archive version”. But any problems during restore are yours to deal with, since downgrading PostgreSQL isn't supported.
You should always use the same version for development and production, and you should always use the latest minor release (currently 10.13). Everything else is asking for trouble.
backup as plain text like this: warning! the file will be huge. Around 17x more than regular custom format. My typical 90mb is now 1.75Gb
copy the backup file into the postgres container docker cp ~/path/to/dump/in-host-system/2020-07-08-1.dump <name_of_postgres_container>:/backups
go to the bash of your postgres container docker exec -it <name_of_postgres_container> bash
inside the bash of postgres container: psql -U username -d dbname < backups/2020-07-08-1.dump
That will work

Configuration issue Postgres on Ubuntu?

I have installed Postgres 12 on Ubuntu by building it from source and I am facing two issues:
Although I followed the installation manual from Postgrez, every time I restart my computer, my Postgres server stopz and is no longer seen as a running process.
To start it the first time after install, I do this from the terminal:
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
After a restart, to start DB again when I run: /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data, it throws this error:
initdb: error: directory "/usr/local/pgsql/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/pgsql/data" or run initdb
with an argument other than "/usr/local/pgsql/data".
Does that mean that every time I start Postgres after a restart, I have to create a new /data directory?
Upon installing Postgres sing pip or pip3, one can just switch user to postgres and run psql to enter postgres, however now I have to run "/usr/local/bin/psql". Please note I have exported all the paths per https://www.postgresql.org/docs/12/installation.html. How can I fix this? Can an alias be set for this?
After a restart, to start DB again when I run:
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data, it throws this
error:
Does that mean that every
time I start Postgres after a restart, I have to create a new /data
directory?
No, quite the opposite. You don't need to initdb after the first time, you just need to start. It is your attempt to initdb when you don't need to which is causing the error message. Note that attempting to initdb isn't doing any harm, because it refused to run. It just generates log/console noise.
Upon installing Postgres sing pip or pip3, one can just switch user to
postgres and run psql to enter postgres, however now I have to run
"/usr/local/bin/psql". Please note I have exported all the paths per
https://www.postgresql.org/docs/12/installation.html. How can I fix
this?
I don't know what your first sentence means, as you don't use pip or pip3 to install PostgreSQL (or at least, the docs don't describe doing so) although you might use them to install psycopg2 to enable python to talk to PostgreSQL.
You could use an alias, but it would probably make more sense to edit ~/.bash_profile to set the PATH, as described from the page you linked to under Environment Variables.
You have to register postgreSQL as a service.
run this:
pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]
Example:
pg_ctl register -N postgresql -U OS_username -P OS_password -D '/etc/postgresql/12/data' -w
More info in the manual: pg_ctl
Notes:
Username and Password is related to the OS, not postgresql
If you have doubts read the manual.
/usr/local/pgsql/bin/pg_ctl start -D '/usr/local/pgsql/data'
Export following in postgres user account's ~/.bashrc:
LD_LIBRARY_PATH=/usr/local/pgsql/lib
export LD_LIBRARY_PATH
PATH=/usr/local/pgsql/bin:$PATH
export PATH

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

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
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?
I'm afraid I will destroy the data in the database or mess it up.
Assuming you've used home-brew to install and upgrade Postgres, you can perform the following steps.
Stop current Postgres server:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Initialize a new 10.1 database:
initdb /usr/local/var/postgres10.1 -E utf8
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
Move new data into place:
cd /usr/local/var
mv postgres postgres9.6
mv postgres10.1 postgres
Restart Postgres:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Check /usr/local/var/postgres/server.log for details and to make sure the new server started properly.
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.
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;
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
If you are using homebrew and homebrew services, you can probably just do:
brew services stop postgresql
brew upgrade postgresql
brew postgresql-upgrade-database
brew services start postgresql
I think this might not work completely if you are using advanced postgres features, but it worked perfectly for me.
Update: This process is the same for upgrading 9.5 through at least 11.5; simply modify the commands to reflect versions 9.6 and 10, where 9.6 is the old version and 10 is the new version. Be sure to adjust the "old" and "new" directories accordingly, too.
I just upgraded PostgreSQL 9.5 to 9.6 on Ubuntu and thought I'd share my findings, as there are a couple of OS/package-specific nuances of which to be aware.
(I didn't want to have to dump and restore data manually, so several of the other answers here were not viable.)
In short, the process consists of installing the new version of PostgreSQL alongside the old version (e.g., 9.5 and 9.6), and then running the pg_upgrade binary, which is explained in (some) detail at https://www.postgresql.org/docs/9.6/static/pgupgrade.html .
The only "tricky" aspect of pg_upgrade is that failure to pass the correct value for an argument, or failure to be logged-in as the correct user or cd to the correct location before executing a command, may lead to cryptic error messages.
On Ubuntu (and probably Debian), provided you are using the "official" repo, deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main, and provided you haven't changed the default filesystem paths or runtime options, the following procedure should do the job.
Install the new version (note that we specify the 9.6, explicitly):
sudo apt install postgresql-9.6
Once installation succeeds, both versions will be running side-by-side, but on different ports. The installation output mentions this, at the bottom, but it's easy to overlook:
Creating new cluster 9.6/main ...
config /etc/postgresql/9.6/main
data /var/lib/postgresql/9.6/main
locale en_US.UTF-8
socket /var/run/postgresql
port 5433
Stop both server instances (this will stop both at the same time):
sudo systemctl stop postgresql
Switch to the dedicated PostgreSQL system user:
su postgres
Move into his home directory (failure to do this will cause errors):
cd ~
pg_upgrade requires the following inputs (pg_upgrade --help tells us this):
When you run pg_upgrade, you must provide the following information:
the data directory for the old cluster (-d DATADIR)
the data directory for the new cluster (-D DATADIR)
the "bin" directory for the old version (-b BINDIR)
the "bin" directory for the new version (-B BINDIR)
These inputs may be specified with "long names", to make them easier to visualize:
-b, --old-bindir=BINDIR old cluster executable directory
-B, --new-bindir=BINDIR new cluster executable directory
-d, --old-datadir=DATADIR old cluster data directory
-D, --new-datadir=DATADIR new cluster data directory
We must also pass the --new-options switch, because failure to do so results in the following:
connection to database failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/lib/postgresql/.s.PGSQL.50432"?
This occurs because the default configuration options are applied in the absence of this switch, which results in incorrect connection options being used, hence the socket error.
Execute the pg_upgrade command from the new PostgreSQL version:
/usr/lib/postgresql/9.6/bin/pg_upgrade --old-bindir=/usr/lib/postgresql/9.5/bin --new-bindir=/usr/lib/postgresql/9.6/bin --old-datadir=/var/lib/postgresql/9.5/main --new-datadir=/var/lib/postgresql/9.6/main --old-options=-cconfig_file=/etc/postgresql/9.5/main/postgresql.conf --new-options=-cconfig_file=/etc/postgresql/9.6/main/postgresql.conf
Logout of the dedicated system user account:
exit
The upgrade is now complete, but, the new instance will bind to port 5433 (the standard default is 5432), so keep this in mind if attempting to test the new instance before "cutting-over" to it.
Start the server as normal (again, this will start both the old and new instances):
systemctl start postgresql
If you want to make the new version the default, you will need to edit the effective configuration file, e.g., /etc/postgresql/9.6/main/postgresql.conf, and ensure that the port is defined as such:
port = 5432
If you do this, either change the old version's port number to 5433 at the same time (before starting the services), or, simply remove the old version (this will not remove your actual database content; you would need to use apt --purge remove postgresql-9.5 for that to happen):
apt remove postgresql-9.5
The above command will stop all instances, so you'll need to start the new instance one last time with:
systemctl start postgresql
As a final point of note, don't forget to consider pg_upgrade's good advice:
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
./analyze_new_cluster.sh
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
The user manual covers this topic in depth. You can:
pg_upgrade in-place; or
pg_dump and pg_restore.
If in doubt, do it with dumps. Don't delete the old data directory, just keep it in case something goes wrong / you make a mistake; that way you can just go back to your unchanged 9.3 install.
For details, see the manual.
If you're stuck, post a detailed question explaining how you're stuck, where, and what you tried first. It depends a bit on how you installed PostgreSQL too, as there are several different "distributions" of PostgreSQL for OS X (unfortunately). So you'd need to provide that info.
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.
Stop current Postgres server:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Initialize a new 9.4 database:
initdb /usr/local/var/postgres9.4 -E utf8
Install postgres 9.3 (as it was no longer present on my machine):
brew install homebrew/versions/postgresql93
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
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/
Move new data into place:
cd /usr/local/var
mv postgres postgres9.3
mv postgres9.4 postgres
Restart Postgres:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Check /usr/local/var/postgres/server.log for details and to make sure the new server started properly.
Finally, re-install related libraries?
pip install --upgrade psycopg2
gem uninstall pg
gem install pg
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
....
On Windows I kept facing different errors messages when trying to use pg_upgrade.
Saved a lot of time for me to just:
Backup DB
Uninstall all copies of PostgreSQL
Install 9.5
Restore DB
This did it for me.
https://gist.github.com/dideler/60c9ce184198666e5ab4
Short and to the point. I honestly don't aim to understand the guts of PostgreSQL, I want to get stuff done.
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”
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 :)
For Mac via homebrew:
brew tap petere/postgresql,
brew install <formula> (eg: brew install petere/postgresql/postgresql-9.6)
Remove old Postgres:
brew unlink postgresql
brew link -f postgresql-9.6
If any error happen, don't forget to read and follow brew instruction in each step.
Check this out for more: https://github.com/petere/homebrew-postgresql
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
I think this is best link for your solution to update postgres to 9.6
https://sandymadaan.wordpress.com/2017/02/21/upgrade-postgresql9-3-9-6-in-ubuntu-retaining-the-databases/

how to fix postgres after updating/upgrading brew

I upgraded to mavericks and had some trouble installing/compiling new gems so I reinstalled xcode and did a brew update and upgrade. Gems work now, and even postgres continued to work for a while until a recent reboot. Now postgres seems to be having issues.
postgres:
postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.
brew info postgres:
postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.2.4 (2842 files, 39M)
Built from source
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 39M) *
Poured from bottle
postgres -D /usr/local/var/postgres:
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.3.2.
What should I do now to get my database working again?
$ brew tap homebrew/versions
$ brew install postgresql92
$ brew switch postgresql 9.2.8 # might need to check this one, could be newer by the time you read this
$ pg_dumpall > ninedottwo-dump
$ brew uninstall postgresql92
$ brew switch postgresql 9.3.4 # again, check version
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
$ createdb # i got an error about my database not existing, so i had to run this
$ psql < ninedottwo-dump
$ bundle exec rails s
Thanks to Peter for getting me started in the right direction.
edit: this'll teach me to upgrade everything all at once...
You need to get a copy of PostgreSQL 9.2 installed somehow, to be able to access the existing data directory.
Options for installing 9.2 via Homebrew include:
Get a checkout of Homebrew from before the postgresql formula was upgraded to 9.3.
Install postgresql92 from homebrew/versions tap.
Install postgresql-9.2 from petere/postgresql tap (disclosure: that's my project).
Install from source.
Then you ought to upgrade that to 9.3 using either pg_dump or pg_upgrade.
pg_upgrade comes for this very purpose, and using it is straightforward. Just make sure you're specifying the right paths for the old data and binaries ( -d and -b ) and the right paths to the new data and binaries (-D and -B):
pg_upgrade \
-d /usr/local/var/postgres/9.2/ -D /usr/local/var/postgres/9.3/ \
-b /usr/local/Cellar/postgresql/9.2.4/bin/ -B /usr/local/Cellar/postgresql/9.3.2/bin/
If everything goes fine, you can then cleanup old versions with brew cleanup postgresql,
and since a new version of PostgreSQL means new versions of the native libraries that are used by the Ruby pg gem, you may want to recompile it:
gem uninstall pg
ARCHFLAGS="-arch x86_64" gem install pg
For 9.2 to 9.5, on El Capitan, I ran into a couple problems which might help others.
It seems now the name of the package used in the switch statement has changed to include the version number (postgresql92 vs postgresql). So, I needed to have the added step of unlinking the current version of postgres:
brew unlink postgresql
brew link postgresql92
brew switch postgresql92 9.2.13 # match version
In order to perform the dump I needed to start the postgres server,
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
But then I ran into the dreaded:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I check out the logs and found this:
could not open directory "pg_tblspc": No such file or directory
According to this answer by Donovan, Yosemite and El Capitan remove some needed directories. So I also needed to add those directories back in using this awesome command by Nate
mkdir /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/
So the full updated version is:
brew tap homebrew/versions
brew install postgresql92
brew unlink postgresql
brew switch postgresql92 9.2.13 # might need to check this one, could be newer by the time you read this
mkdir /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
pg_dumpall > ninedottwo-dump
pg_ctl -D /usr/local/var/postgres stop
brew unlink postgresql92
brew uninstall postgresql92
brew switch postgresql 9.5.2 # again, check version
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
createdb # i got an error about my database not existing, so i had to run this
psql < ninedottwo-dump
bundle install # need to make sure you have pg gem for 9.5
bundle exec rails s
All credit goes to sentinel, Nate, and Donovan. Thanks so much!