How do I import modules or install extensions in Postgres? - postgresql

I'm trying to import several modules that come bundled with postgres, and all the commands to do so (such as contrib.import etc) do not work or cannot be found.

To install PostgreSQL contrib modules on Ubuntu or Kubuntu (or similar Linux distributions):
Install the contrib package:
sudo apt-get install postgresql-contrib
Change to the database owner account (e.g., postgres).
CREATE EXTENSION "uuid-ossp";
If you are trying to install non-"trusted" modules, you need to be a superuser to install them. Otherwise, you only need to have CREATE privilege on the database you are trying to use the module on.
For versions before 9.1, do step #1 above, and then:
Restart the database:
sudo /etc/init.d/postgresql-8.4 restart
Change to the database owner account (e.g., postgres).
Change to the contrib modules' directory:
/usr/share/postgresql/8.4/contrib/
Use ls to see a list of the following modules:
adminpack autoinc
btree_gin btree_gist
chkpass citext
cube dblink
dict_int dict_xsyn
earthdistance fuzzystrmatch
hstore insert_username
int_aggregate isn
lo ltree
moddatetime pageinspect
pg_buffercache pgcrypto
pg_freespacemap pgrowlocks
pg_stat_statements pgstattuple
pg_trgm pgxml
refint seg
sslinfo tablefunc
test_parser timetravel
tsearch2 uuid-ossp
Load the SQL files using:
psql -U user_name -d database_name -f module_name.sql
For example, if your administrative user was named postgres and your database was named storage and the module you wanted was cube, you would type:
psql -U postgres -d storage -f cube.sql

login as postgres user
use create extension to load it
I have a database named 'book' for example,
psql -U postgres book
create extension cube
Repeat for each extension required, then \q to logouy

Related

Can not install hstore extension to new created schema

I have an ubuntu 18.04 with postgres 9.5 installed.
My db "mydb" has the hstore installed. When I do "\dx store", I do have
List of installed extensions
Name | Version | Schema | Description
--------+---------+--------+--------------------------------------------------
hstore | 1.3 | public | data type for storing sets of (key, value) pairs
(1 row)
When I do a pg_restore with a certain backup file, a new schema also called "mydb" is created, but it does not contain the "hstore" extension. The result of the "\dx" command is the same. hstore is in my template1 already.
The pg_restore fails with
pg_restore: [archiver (db)] could not execute query: ERROR: type "hstore" does not exist
Can anyone point out where the problem is?
Thanks
Was the backup created with the same version of postgresql?
The dump / restore cycle generally assumes that the database was created in advance, but the dump should create the extensions.
Can you search the backup to see if it contains a directive to create the extension for your schema:
CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA public;
you will need to use the pg_restore command to stdout e.g.
pg_restore mydump.dump|more
You may also need to check that your schema is created before the extension, if you are not using the public schema.
Can you try the following to create the hstore extension in your mydb schema before the restore:
CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA mydb;
Alternatively, the hstore extension may not be installed properly.
# locate hstore|grep postgresql
# dpkg -S /path/to/hstore.so

Postgresql: Can't access to file « $libdir/postgis-2.1 » no such file or directory

I guess this is a known issue but since I have used a script that removed my postgresql-9.4-postgis-2.1, I'm now unable to get rid of this SQL error under Debian.
Can't access to file « $libdir/postgis-2.1 » no such file or directory
I've done the following:
- Remove new unwanted postgresql-9.5-postgis-2.2 package installed
- Reinstalling postgresql-9.4-postgis-2.1, postgresql-9.4-postgis-scripts and postgis
- Using SQL: ALTER EXTENSION postgis UPDATE TO '2.1.4' --under postgres user
- Using SQL: ALTER EXTENSION postgis_topology UPDATE TO '2.1.4' --under postgres user
And SELECT * FROM pg_available_extensions; returns
[...]
postgis 2.1.4 2.1.4 PostGIS geometry, geography, and raster spatial types and functions.
But still this message when accessing object like tables that uses geometry type.
Any idea?
Finally "solve" the issue compiling from source postgis 2.2.2 (that also requires gdal, proj4 and geos) and then issuing
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
since DROP EXTENSION postgis; tells me postgis wasn't present anymore.
And finally:
ALTER EXTENSION postgis UPDATE TO '2.2.2';
ALTER EXTENSION postgis_topology UPDATE TO '2.2.2';
Got access again to my geom data and functions from PostgreSQL 9.4. It seems that compiling from source updated correctly the variable path for postgresql and installed /usr/lib/postgresql/9.4/lib/postgis-2.2.so that wasn't present anymore (for postgis-2.1.so as well), even with (re)installing from apt-get.
Hope this will help.
First run (as postgres administrator) in the database that needs it (\c your-database-name):
ALTER EXTENSION postgis UPDATE;
If this returns successfully, then check what version you have
SELECT PostGIS_Full_Version();
If however, the server returns that it cannot find the extension postgis, then run
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
ALTER EXTENSION postgis UPDATE;
and again check by running
SELECT PostGIS_Full_Version();
If you are using ubuntu
Enter in postgres interactive shell by using below command
sudo -u postgres psql
\c database_name;
ALTER EXTENSION postgis UPDATE TO "2.4.4";
SELECT PostGIS_Full_Version();

Installing extension postgresql 9.4 for all schemes

How to install an extension pg_stat_statements once for all schemes in a single database postgresql 9.4?
CREATE EXTENSIONS pg_stat_statements;
installs only in the public schema
UPD:
I solved the problem as follows:
sudo -u postgres psql -d "template1" -c "CREATE EXTENSION pg_stat_statements;"
sudo -u postgres psql -d "template1" -c "ALTER EXTENSION pg_stat_statements SET SCHEMA pg_catalog;"
The newly created database will have an extension in pg_catalog scheme that will allow this extension to work with all other schemes in the database.
As given here, although the Extension installs in Public by default, you can change and install it on any Schema using the given syntax.
From your question it seems that you need to install the extension in all Schemas.... Please note that you don't need to install a given Extension in all Schemas (within a Database). The Schema is just a placeholder for all the DB objects, but once installed, the Extension as such, is available to all Schemas in the Database (and needn't be installed for each Schema).

cmd after createdb command with postgresql

I just installed ubuntu 14.04 and i'm trying to learn about creating databases using postgresql but i can't even create a simple testing table. I'm checking guides for about 3 hours now on how to start but with my little knowledge about ubuntu i don't understand much and thats why i try asking here. I will write the steps i follow because i'm not sure where the mistake is. I installed postgresql using the commands
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
After that i use
sudo -i -u postgres
to log in to the autocreated user account postgres and then
psql
to be able to interact with the db management system.
After that i write
createdb testingdb
in order to make the first step and then \l to check if it is created but the outcome of \l is a list of 3 databases (3 rows postgres template0 template 1) without the testingdb being there. Also at this point the cmd is frozen and looks likes this
the list of the 3 databases
(3 rows)
~
~
~
~
~
~
(END)
Any hint will be really helpfull.
I think you're mixing two separate ways of creating a database:
Method 1
psql #(Type this on the shell prompt)
CREATE DATABASE testingdb;
\c testingdb
CREATE TABLE tbl1(a INT);
\q
Method 2
createdb testingdb #(Again, type this on the shell prompt, not inside psql!)
psql testingdb
CREATE TABLE tbl1(a INT);
\q
The last bit about stuck prompt, is because you probably are inside the psql prompt, and to come back to the shell prompt, you need to type \q (or Control-d).

How do I add PostGIS to PostgreSQL pgAdmin?

I've got PostgreSQL and pgAdmin installed, but I want to add a PostGIS server so I could work on a Geographic Informations System project.
I'm following this tutorial, which assumes PostGIS is set up. In the previous page of the tutorial, it instructs you to download their software package which includes PostgreSQL and pgAdmin. I already have those installed, so I would like to just add PostGIS but I don't see any way to do so.
Any ideas?
Connect to the database using a superuser account (most often the user named postgres but in more recent versions the id of the user who installed the application.) Then issue the following SQL commands to enable PostGIS functionality.
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
You can do this from within pgAdmin or via psql -U [superuser] [database] from a command line.
Alternately for the command adverse; as a superuser; from within pgAdmin right click on your database's Extensions and select New Extension. Then in the drop down associated with Name select the postgis* extensions needed.
Before adding PostGIS extension to Postgres. You first need to install PostGIS on Ubuntu 14.04
Add PPA from terminal
$ sudo add-apt-repository ppa:ubuntugis/ppa
$ sudo apt-get update
Install PostGIS with apt-get
$ sudo apt-get install postgis postgresql-10-postgis-2.5
After successful installation open psql
$ sudo -u postgres psql
List all databases
postgres=# \l
Connect to specific database
postgres=# \c DATABASE_NAME
Run the following to add PostGIS extension to Postgres
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
Exit from psql
postgres=# \q
Thank you
You don't install it to pgAdmin, it is an extension to Postgres itself. Once it is installed, you add it to a particular database, and its functions, nearly 1000, a few views and the spatial_ref_sys table, will appear in pgAdmin in that db. You can also install it to the template1 db, and then it will be automatically included in any other database that you subsequently create. You can get a binary install from http://postgis.net/install/
For PGAdmin installation of extensions:
Right click on the database name listed under the server cascade list, you will see an option "Create Script" - click on it.
This opens up a script with some information on creation/alteration of the DB. Clear this script and then paste the following lines:
CREATE EXTENSION postgis;
Look at the icons at the top - you will see a RUN icon/button - looks like a "play" icon. Click on it.
Look at the Log output, it should have successfully run the command and installed the extension.