Change sequence name in postgreSQL

ALTER SEQUENCE id_seq_player RENAME TO player_id_seq; is correct. You might want to add the schema name as well to ensure you are altering the correct one, but this should most likely work. If it timeouts, you might have another process that is locking your sequence. Is there a way for you to turn off all … Read more

PostgreSQL compare two jsonb objects

UPDATED CREATE OR REPLACE FUNCTION jsonb_diff_val(val1 JSONB,val2 JSONB) RETURNS JSONB AS $$ DECLARE result JSONB; v RECORD; BEGIN result = val1; FOR v IN SELECT * FROM jsonb_each(val2) LOOP IF result @> jsonb_build_object(v.key,v.value) THEN result = result – v.key; ELSIF result ? v.key THEN CONTINUE; ELSE result = result || jsonb_build_object(v.key,’null’); END IF; END LOOP; … Read more

Postgres on Heroku and dumping single table to dump file

You can dump a single table of data like so: $ pg_dump –no-acl –no-owner -h [host ip].compute-1.amazonaws.com -U [user name] -t [table name] –data-only [database name] > table.dump You can get all of the values needed with this: $ heroku pg:credentials:url [DATABASE] -a [app_name] Connection info string: “dbname=[database name] host=[host ip].compute-1.amazonaws.com port=5432 user=[user name] password=[password] … Read more

Performance Tuning: Create index for boolean column

For a query like this, a partial index covering only unsynced rows would serve best. CREATE INDEX ON tbl (id) WHERE sync_done = FALSE; However, for a use case like this, other synchronization methods may be preferable to begin with: Have a look at LISTEN / NOTIFY. Or use a trigger in combination with dblink … Read more

PostgreSQL/JDBC and TIMESTAMP vs. TIMESTAMPTZ

Generally use TIMESTAMPTZ Here’s advice from David E. Wheeler, a Postgres expert, in a blog post whose title says it all:Always Use TIMESTAMP WITH TIME ZONE (TIMESTAMPTZ) If you are tracking actual moments, specific points on the timeline, use TIMESTAMP WITH TIME ZONE. One Exception: Partitioning Wheeler’s sole exception is when partitioning on timestamps, because … Read more

Select multiple ids from a PostgreSQL sequence

select nextval(‘mytable_seq’) from generate_series(1,3); generate_series is a function which returns many rows with sequential numbers, configured by it’s arguments. In above example, we don’t care about the value in each row, we just use generate_series as row generator. And for each row we can call nextval. In this case it returns 3 numbers (nextvals). You … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)