I try to set my start increment value but I "get relation does not exist". I have a table named student and a column named student_id.
ALTER SEQUENCE student_student_id_seq RESTART WITH 18354001
I tried to use double and single quotes but it does not work.
I know that is simple and so general question but I could not handle it.
You can find the sequence associated to a given table/column using
select pg_get_serial_sequence('myschema.mytable','mycolumn');
Related
Let's say I have a table Student with just 2 columns - id bigint, name varchar(50).
I'm trying to rename it and add column in a same single query in PostgreSQL version 11.16 like below:
ALTER TABLE student
RENAME COLUMN name TO fullname,
ADD COLUMN roll_no varchar(30);
But I got this error:
Syntax error at or near "ADD"
Is it possible? If not, why?
Actually, that is not possible. I got this from the StackOverflow answer. One RENAME is possible for only one statement. Even multiple RENAME commands are also not possible. Here is the postgres manual.
Could you please help me to choose SERAIL vs Sequence in project..
Which is best for future use?
I'm using POSTGRESQL13 for project Can I use SERAIL type
There is no serial type. Using serial is more like a macro, which creates a column of type integer, and a sequence to fill it. If you describe the table it \d or dump the create command of a table created with serial, you will see it not described/dumped as serial.
One thing that using serial does it mark the sequence as being owned by the column, so that it will get dropped if the column gets dropped. You can accomplish this after-the-fact by running something like this:
ALTER SEQUENCE public.x_x_seq OWNED BY public.x.x;
For new work happening in v13 (or back to v10) you might consider using generated always as identity instead.
I have a postgres table with two columns (an identificator and a date) that are a composite primary key. I would like to hash the concatenation in another column, generating this value everytime a new record is inserted. For that I'm trying to alter my table in order to create a generated column:
ALTER TABLE my_table ADD COLUMN hash_id_date VARCHAR(50)
GENERATED ALWAYS AS (MD5(my_table.original_id||'-'||my_table.time))
STORED;
This raises me the following error:
ERROR: syntax error at or near "("
LINE 4: GENERATED ALWAYS AS (MD5(my_table.original_id,'-',my_table.t...
^
SQL state: 42601
Character: 178
I'm turning into madness to find where is the syntax error... I've read about STABLE and IMMUTABLE functions and generated columns should always have an IMMUTABLE function as expression. As far as I know MD5 is IMMUTABLE but the error message is not even capable to reach that level.
Any help?
Assuming the basic functionality for calculating the MD5 is common you can create a function for the calculation. Use this function wherever it's needed, including updating your current rows and invoke from a trigger on yo your table. If the particular MD5 calculation is not all that common you can just put the calculation in the trigger function and also use it in a independent update for current rows. See here for example with assumption it is common in your app.
I am trying to increment sequence on id of table. But I don't know sequence name because it's default sequence that postgresql generated.
I have read questions like these:
ALTER postgreSQL sequence
but they don't really answer my question because I don't know name of sequence. I tried:
ALTER SEQUENCE table_name_id_seq INCREMENT BY 1;
but it didn't work.
You can retreive the name of the sequence from the table/column names.
Then you can use setval to set the current value. In the example below, it sets the value to the biggest found value (next one will be +1)
SELECT setval(
pg_get_serial_sequence('myschema.mytable','mycolumn'),
max(mycolumn))
FROM myschema.mytable;
I need to make a function that would be triggered after every UPDATE and INSERT operation and would check the key fields of the table that the operation is performed on vs some conditions.
The function (and the trigger) needs to be an universal one, it shouldn't have the table name / fields names hardcoded.
I got stuck on the part where I need to access the table name and its schema part - check what fields are part of the PRIMARY KEY.
After getting the primary key info as already posted in the first answer you can check the code in http://github.com/fgp/pg_record_inspect to get record field values dynamicaly in PL/pgSQL.
Have a look at How do I get the primary key(s) of a table from Postgres via plpgsql? The answer in that one should be able to help you.
Note that you can't use dynamic SQL in PL/pgSQL; it's too strongly-typed a language for that. You'll have more luck with PL/Perl, on which you can access a hash of the columns and use regular Perl accessors to check them. (PL/Python would also work, but sadly that's an untrusted language only. PL/Tcl works too.)
In 8.4 you can use EXECUTE 'something' USING NEW, which in some cases is able to do the job.