PostgreSQL ,Postgis and Pgrouting compatible versions - postgresql

I am trying to work with PostgreSql 9.3, Postgis 2.1.3 and Pgrouting 2.0.0 versions in Ubuntu (VPS). I have installed PostgreSql 9.3,but when i run CREATE EXTENSION postgis SCHEMA public VERSION "2.0.0"; in PgAdmin3 i get ERROR: could not stat file "/usr/share/postgresql/9.3/extension/postgis--2.1.3.sql": No such file or directory and same error with Pgrouting 2.0.0.Are these versions compatibles running in that system?,in localhost work fine.

sudo apt-get update
sudo apt-get install postgresql-9.3 postgresql-9.3-postgis-2.1 postgresql-9.3-postgis-scripts postgresql-contrib-9.3 postgresql-client-common postgresql-common postgresql-server-dev-9.3 postgresql-client-9.3
This should install everything and start the server. Read the following man pages. to create a user and a database.
man createuser
man createdb
and this will install postgis into database 'mydatabase' for user 'user':
psql -U user mydatabase -c "create extension postgis"

Related

psql still exists despite uninstalling

I installed psql on my system without having added any new package sources. Later,I added postgres's package repo following the instructions on their website and ran sudo apt install postgres. I then ran sudo apt remove postgres.
Now, when I run sudo apt remove postgresql again, apt tells me "Unable to locate package postgresql". However, running dpkg -l | grep sql yields (among other things):
ii postgresql-14 14.5-0ubuntu0.22.04.1 amd64 The World's Most Advanced Open Source Relational Database
ii postgresql-15 15.1-1.pgdg22.04+1 amd64 The World's Most Advanced Open Source Relational Database
ii postgresql-client-14 14.5-0ubuntu0.22.04.1 amd64 front-end programs for PostgreSQL 14
ii postgresql-client-15 15.1-1.pgdg22.04+1 amd64 front-end programs for PostgreSQL 15
ii postgresql-client-common 246.pgdg22.04+1 all manager for multiple PostgreSQL client versions
ii postgresql-common 246.pgdg22.04+1 all PostgreSQL database-cluster manager
And running psql --version from the command line yields psql (PostgreSQL) 15.1 (Ubuntu 15.1-1.pgdg22.04+1).
Here are some questions, info about any is appreciated:
Why can't apt find the postgresql package even though psql is installed?
What package is psql part of and how do I figure that out? Does psql still exist on my system because it is part of a package that another package I have installed depends on?
I'm just interested in using the most recent LTS version of postgres (15.1 at the time I'm writing this). Do I need the version 14 packages which exist on my systems? If not, how do I remove them?
Edit: After sudo apt removeing postgresql-14 and postgresql-15, psql still exists on my system but gives the following error: psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory. Why does the psql command still work?

How to upgrade postgresql database from 10 to 12 without losing data for openproject

My OpenProject management software is installed with default postgresql 10.
Currently the postgresql DB is 12, It is having lot of new features.
I want to upgrade my Postgres DB without losing the data in the DB.
My system is ubuntu 18.04 and hosted openproject.
I searched the internet and could not find a step by step to upgrade postgresql.
Can you please guide me to install new DB and all data should be in the new DB.
thanks for your help.
A) First create a backup of all the databases for that (You can continue from B if you dont need a backup)
Log in as postgres user
sudo su postgres
Create a backup .sql file for all the data you have in all the databases
pg_dumpall > backup.sql
B) Upgrade to PostgreSQL12
update packages and install postgres 12
sudo apt-get update
sudo apt-get install postgresql-12 postgresql-server-dev-12
Stop the postgresql service
sudo systemctl stop postgresql.service
migrate the data
/usr/lib/postgresql/12/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/10/main \
--new-datadir=/var/lib/postgresql/12/main \
--old-bindir=/usr/lib/postgresql/10/bin \
--new-bindir=/usr/lib/postgresql/12/bin \
--old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \
--new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf'
Switch to regular user
exit
Swap the ports the old and new postgres versions.
#change port to 5432
sudo vim /etc/postgresql/12/main/postgresql.conf
#change port to 5433
sudo vim /etc/postgresql/10/main/postgresql.conf
Start the postgresql service
sudo systemctl start postgresql.service
Log in as postgres user
sudo su postgres
Check your new postgres version
psql -c "SELECT version();"
Run the generated new cluster script
./analyze_new_cluster.sh
Return as a normal(default user) user and cleanup up the old version's mess
#uninstalls postgres packages
sudo apt-get remove postgresql-10 postgresql-server-dev-10
#removes the old postgresql directory
sudo rm -rf /etc/postgresql/10/
#login as postgres user
sudo su postgres
#delete the old cluster data
./delete_old_cluster.sh
Congrads! Your postgresql version is now upgraded, If everything works well in B, we dont have to apply the backup as we have already migrated the data from the older version to the newer version, the backup is just in case if anything goes wrong.
NOTE: Change the postgresql.conf and pg_hba.conf as per your requirement
PS: Feel free to comment your issues, suggestions or anyother modifications you would like to suggest
Backup the database
psql --version sudo -u postgres psql pg_dumpall > alldbs.sql
(this command will backup all databases from the postgresql db)
Then exit from postgres user, and:
Inside a terminal run these commands:
sudo systemctl stop postgres
sudo apt-get install -y postgresql-12 postgresql-server-dev-12 postgresql-contrib-12 libpq-dev postgresql-12-hypopg
sudo pg_dropcluster 12 main --stop
sudo pg_upgradecluster 10 main
sudo pg_dropcluster 10 main --stop
restart the postgresql service:
sudo systemctl restart postgresql
login to the postgres:
su - postgres
to check the version:
psql --version
I have done using the above steps and I could update the DBand restore all data.
Postgresql Upgrade using pg_upgrade
Step - 1 (Find the locale language and encoding of source Postgresql server)
Log in to Postgresql database as postgres user
Execute the query SHOW LC_COLLATE;
Step - 2 ( Stop the existing/source Postgresql server)
Find the running Postgresql service using $ sudo systemctl list-units | grep postgres
Stop the service $ sudo service postgresql-<source-version>.service stop
Step - 3 (Install target Postgresql server)
Configure repos and install (lot of tutorials available in google)
Step - 4 (Update the locale language and encoding)
$ /usr/pgsql-<target-version>/bin/initdb -D /var/lib/pgsql/<target-version>/data --locale=<add-your-encoding>
Step - 5 (Check the source to target upgrade has any potential issues)
Run the command as postgres user, $sudo su postgres
$ /usr/pgsql-<target-version>/bin/pg_upgrade --old-bindir /usr/pgsql-<source-version>/bin --new-bindir /usr/pgsql-<target-version>/bin --old-datadir /var/lib/pgsql/<source-version>/data --new-datadir /var/lib/pgsql/12/data --link --jobs=2 --check
If it's ok we can move to the next step, If not fix the issues before proceeding to the next step
Expected output: Clusters are compatible
Step - 6 (Upgrade the source to target Postgresql version using the link option)
link option is pretty faster than the migration
Run the command as postgres user, $sudo su postgres
$ /usr/pgsql-<target-version>/bin/pg_upgrade --old-bindir /usr/pgsql-<source-version>/bin --new-bindir /usr/pgsql-<target-version>/bin --old-datadir /var/lib/pgsql/<source-version>/data --new-datadir /var/lib/pgsql/<target-version>/data --link
Expected output: Upgrade Complete
Thanks.
ENJOY !!!

No passwd entry for user 'postgres' error

I'm trying to setup Postgres on Ubuntu 14.04 without success. After running
sudo apt-get install libpq-dev python-dev
and
sudo apt-get install postgresql postgresql-contrib
I've managed to install the packages successfully. However upon executing sudo su - postgres, I'm receiving the following error:
No passwd entry for user 'postgres'
It seems like the installation did not create the user postgres as it is not in the /etc/passwd file. Reinstalling the package does not resolve the issue. How can I add the required postgres user?
It sounds like you want the PostgreSQL server:
apt-get install postgresql-server
Also, you never need to use sudo su - postgres. Just:
sudo -u postgres -i
to get an interactive shell, or
sudo -u postgres psql
(or whatever) to run a command.
I've solved the issue by purging and re-installing the packages, i.e.
sudo apt-get --purge remove postgresql*
sudo apt-get install postgresql postgresql-contrib

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!

cannot find adminpack.sql in postgresql 9.1

I'm trying to get it installed and use in ubuntu. Was following this page to install the additional instrumentation packages.
Install Server Instrumentation for Postgresql 8.4
To install Server Instrumentation for Postgresql 8.4 you may use the command line and type:
sudo apt-get install postgresql-contrib
Run the adminpack.sql script, simply type:
sudo -u postgres psql < /usr/share/postgresql/8.4/contrib/adminpack.sql
But I couldn't find adminpack.sql in 9.1 version.
In PostgreSQL 9.1 and later, extensions can be installed via the CREATE EXTENSION command:
sudo -u postgres psql
CREATE EXTENSION "adminpack";
Try locate adminpack. But first, run updatedb to make sure the locate database is up to date.
sudo updatedb
locate adminpack
The output is:
/usr/lib/postgresql/9.1/lib/adminpack.so
/usr/share/postgresql/9.1/extension/adminpack--1.0.sql
/usr/share/postgresql/9.1/extension/adminpack.control
Looks like this is old and comments are closed, so I'm opening another answer.
ldalonzo's answer is correct once you have all the components, but I found I also had to run:
sudo apt-get install postgresql-contrib-9.1
Looks like the bare postgresql package doesn't include the additional modules.
Once you install postgresql-contrib using below command
sudo apt-get install postgresql-contrib
Adminpack is located in the below path
/usr/share/postgresql/9.1/extension/adminpack--1.0.sql
To load the adminpack use the below command
sudo su postgres -c psql < /usr/share/postgresql/9.1/extension/adminpack--1.0.sql
Then
sudo /etc/init.d/postgresql restart