How to use extensions in Postgres installed with Homebrew on Mac OSX - postgresql

I'd like to use the SPI extension in Postgres 10.2, which I installed with Homebrew. However,
CREATE EXTENSION spi;
fails with
ERROR: could not open extension control file "/usr/local/share/postgresql/extension/spi.control": No such file or directory
Looking inside that extension directory, I see many extensions, but not SPI. The Postgres documentation mentions that extensions would reside in a contrib directory of the distribution and that you can then build them individually, but I can't seem to find this directory anywhere. Any idea how I can obtain and install the SPI module?

https://www.postgresql.org/docs/current/static/contrib-spi.html
Each of the groups of functions described below is provided as a
separately-installable extension.
so you check and try:
t=# select * from pg_available_extensions where name in ('refint','timetravel','autoinc','insert_username','moddatetime');
name | default_version | installed_version | comment
-----------------+-----------------+-------------------+-------------------------------------------------------------
moddatetime | 1.0 | | functions for tracking last modification time
autoinc | 1.0 | | functions for autoincrementing fields
insert_username | 1.0 | | functions for tracking who changed a table
timetravel | 1.0 | | functions for implementing time travel
refint | 1.0 | | functions for implementing referential integrity (obsolete)
(5 rows)
t=# create extension refint ;
CREATE EXTENSION
t=# create extension timetravel;
CREATE EXTENSION
and so on...

Related

Why do certain psql commands from terminal work for local database and not for hosted database?

I have imported a local PostgreSQL database to a managed cluster on Digital Ocean. It will be used with a Python app that will also be hosted on Digital Ocean. I used pg_dump and pg_restore to achieve the import. Now, to make sure the import was successful, I am running some psql queries and commands via my MacOS terminal app that is set up with zsh and it connects via a shell script that prompts me for host, database name, port, user and password. I am successful in connecting to the managed cluster this way, and I can execute some queries with no problem, while others are causing errors. For example:
my_support=> \dt
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+---------
public | ages | table | doadmin
public | articles | table | doadmin
public | challenges | table | doadmin
public | cities | table | doadmin
public | comments | table | doadmin
public | messages | table | doadmin
public | relationships | table | doadmin
public | topics | table | doadmin
public | users | table | doadmin
(9 rows)
my_support=> \dt+
sh: more: command not found
my_support=>
Also:
my_support=> SELECT id,sender_id FROM messages;
id | sender_id
----+-----------
1 | 1
2 | 2
3 | 4
4 | 1
5 | 2
(5 rows)
my_support=> SELECT * FROM messages;
sh: more: command not found
my_support=>
So the terminal app seems to dislike certain characters, such as * and +, but I can't find any documentation that tells me I should escape them, or how. I tried backslash in front of them, but it did not work. And what's more confusing is that these very same queries are successful when I connected to my LOCAL copy of the database, using the very same terminal app, launched from the very same shell script.
In case it's helpful, here's what I see in the CLI when I connect:
psql (14.1, server 14.2)
SSL connection (protocol: TLSv1.3, cipher: <alphanumeric string here>, bits: 256, compression: off)
Type "help" for help.
my_support=>
Does it matter that my local PostgreSQL version is 14.1 and the server is 14.2? I'm assuming the "server" refers to the hosted environment, but it seems like something as basic as "SELECT * FROM" should not be version-dependent.
Ultimately what matters is whether my Python app (which uses psycopg library to talk to PostgreSQL) can run those queries, and I haven't test that yet. But it sure would be handy to test things on the managed cluster using my local terminal app.
BTW, I have an open ticket with Digital Ocean to answer this question, but I find SO to be faster and more helpful in most cases.
psql is trying to use a pager to display results that are longer than the number of lines in the terminal. The error message
more: command not found
indicates that the pager (more) it tries to use is not available. You can turn off the use of a pager:
\pset pager off
or set a different command to be used as the pager. See the manual for details

PostgreSQL cannot generate uuid but uuid-ossp extension is installed?

postgresql11-server-11.8_2
When I try to add a new client in idempiere I get the following error message:
idempiere Version=7.1.0.202008290007
Initial Client Setup Process
Do you want to start the Process?
** org.postgresql.util.PSQLException: ERROR: function uuid_generate_v4() does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Where: PL/pgSQL function generate_uuid() line 3 at RETURN
This usually indicates that the uuid extension is missing from the database. But this is not the case.
[root#accounting-2 ~ (master)]# sudo -u postgres psql --dbname=idempiere
psql (11.8)
Type "help" for help.
idempiere=# \dx
List of installed extensions
Name | Version | Schema | Description
-----------+---------+------------+-------------------------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
uuid-ossp | 1.1 | public | generate universally unique identifiers (UUIDs)
What is causing this? How is it fixed?
The output of \dx in my postgres looks like this:
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
uuid-ossp | 1.1 | adempiere | generate universally unique identifiers (UUIDs)
In your case it shows uuid-ossp installed in the public schema, while in my case is installed in adempiere schema.
Please try the following command in your idempiere database:
CREATE EXTENSION "uuid-ossp"

Timescale: ERROR: tried calling catalog_get when extension isn't loaded

I'm using Timescale and today I faced on a problem:
I`m creating a simple table with any of bellow ways:
1-
> create table if not exists "NewTable" as (select * from "OldTable");
SELECT 6
2-
create table "NewTable" ("eventTime" timestamptz, name varchar);
after the table successfully created. I write \d and the result for both tables are same:
Column | Type | Collation | Nullable | Default
-----------+--------------------------+-----------+----------+---------
eventTime | timestamp with time zone | | |
name | character varying | | |
but the problem starts here...
> SELECT create_hypertable('"NewTable"', '"eventTime"' ,migrate_data => true);
ERROR: tried calling catalog_get when extension isn't loaded
so after I googled my issue I found nothing usefull insted of everyone told to create timescaledb extention that I had it before
> CREATE EXTENSION timescaledb CASCADE; //OR CREATE IF NOT EXISTS EXTENSION timescaledb CASCADE;
ERROR: extension "timescaledb" has already been loaded with another version
and
> \dx
Name | Version | Schema | Description
-------------+---------+------------+--------------------------------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
timescaledb | 1.5.1 | public | Enables scalable inserts and complex queries for time-series data
so what should I do?
Question : why this happened? how should I create a hyper table now?
before these operations, I tried to take a dump from my database and before that, I have 20 main_hyper_tables.
why this happened?
I guess TimescaleDB was installed in the standard way through apt. Since new version of TimescaleDB (v.1.6) was released recently, apt automatically updated installation and copied the binary of 1.6 into the shared library of PostgreSQL installation. Thus new extension version was loaded, which is different from the extension used to create the database (v.1.5.1).
how should I create a hyper table now?
I see two options:
Load the extension version used to create the database, by explicitly specifying in the postgres config.
Update the extension to the latest version by
ALTER EXTENSION timescaledb UPDATE
See using ALTER EXTENSION section in Update TimescaleDB doc

Getting ERROR: type "period" does not exist, even i have added btree_gist and temporal_tables extensions

I'm getting error even i have added btree_gist and temporal_tables extensions.
postgres=# \dx
Name | Version | Schema | Description
-----------------+---------+------------+-----------------------------------------------
btree_gist | 1.1 | public | support for indexing common datatypes in GiST
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
temporal_tables | 1.2.0 | public | temporal tables
(3 rows)
CREATE TABLE b (p PERIOD);
ERROR: type "period" does not exist
LINE 1: CREATE TABLE b (p PERIOD);
Version details
postgres=# select version();
version
---------------------------------------------------------------------------------------------
PostgreSQL 9.5.12 on x86_64-alpine-linux-musl, compiled by gcc (Alpine 5.3.0) 5.3.0, 64-bit
If you are talking about this extension, I am not surprised.
The extension does not define a data type or domain called period.

PostgreSQL procedural language "C" not found

I am trying to use the PL/R procedural language in a PostgreSQL 9.2 database. I have installed the plr language and I am trying to add it to a database. When I run the command CREATE EXTENSION plr; I get the following error:
ERROR: language "C" does not exist
STATEMENT: CREATE EXTENSION plr;
ERROR: language "C" does not exist
When I list the available languages in the database with select * from pg_language; I get
lanname | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl
----------+----------+---------+--------------+---------------+-----------+--------------+--------
internal | 10 | f | f | 0 | 0 | 2246 |
c | 10 | f | f | 0 | 0 | 2247 |
sql | 10 | f | t | 0 | 0 | 2248 |
plpgsql | 10 | t | t | 12514 | 12515 | 12516 |
(4 rows)
So there is a language c but it is not in capital letters (not sure if that makes a difference).
I am wondering why the plr extension does not find the C procedural language?
You are probably running into this change in PostgreSQL 9.2 (quoting the release notes here):
No longer forcibly lowercase procedural language names in CREATE
FUNCTION (Robert Haas)
While unquoted language identifiers are still lowercased, strings and
quoted identifiers are no longer forcibly down-cased. Thus for example
CREATE FUNCTION ... LANGUAGE 'C' will no longer work; it must be
spelled 'c', or better omit the quotes.
It's also reflected in the manual for CREATE FUNCTION
lang_name
The name of the language that the function is implemented in. Can be SQL, C, internal, or the name of a user-defined procedural
language. For backward compatibility, the name can be enclosed by
single quotes.
Quoting the language name has been discouraged since at least version 7.3 (maybe longer), but old habits die hard, obviously. Removing the quotes around 'C' fixes the problem, arriving at: LANGUAGE c or LANGUAGE C.
PL/R wasn't ready for PostgreSQL 9.2 in that respect, judging from the project page.
Feedback from Joe Conway
Joe Conway left an answer that got deleted because it should be a comment. I paste it here for the general public who can't see deleted answers:
I got the message, just haven't had the time to do a new PL/R release.
Look for it by December, but in the meantime the manual workaround
noted above is pretty simple.
I had a similar issue with pg_collkey. I believe your situation may have the same solution. Here's what I did: There's a .sql file in your postgres extension directory that tells postgres how to install the extension. In my case, it's called pg_collkey--0.5.0.sql. You'll need to modify that sql file. On my system (using Mac OS X and Homebrew) the file is located at /usr/local/share/postgresql/extension/pg_collkey--0.5.0.sql). It probably contains a capital "C" in the LANGUAGE clause, like so:
CREATE OR REPLACE FUNCTION collkey (text, text, bool, int4, bool) RETURNS bytea
LANGUAGE 'C' IMMUTABLE STRICT AS
'$libdir/collkey_icu.so',
'pgsqlext_collkey';
Change it to a lower-case 'c' and re-run the "CREATE EXTENSION pg_collkey;" command in psql:
psql -c 'CREATE EXTENSION pg_collkey;' my_database_name
Of course, you'll need to use "plr" instead of pg_collkey for the extension name.