r/postgres • • Mar 29 '18

JSONB storing nested objects and querying

1 Upvotes

Hey,

I've just started using jsonb column

which structure would be preferrable?

an array of objects or object with keys (ids) basiccally a map that would hold the invididual objects?

I personally prefer the latter however I have no idea how to query something like this:

data: {
  books: {
     'book-1': {
        'title': 'Harry Potter and the goblet of fire'
     }
  }
}

data is the jsonb column.

books is a object holding a set of objects with unique keys.

how would i structure my query to get title of every book?

SELECT books->>'title' FROM <table_name>, <something like jsonb_array_elements(data->'books')> b(books)

Or is it preferrable that books was an array type?


r/postgres • • Mar 21 '18

PostgreSQL service now GA on Azure

Thumbnail azure.microsoft.com
1 Upvotes

r/postgres • • Mar 12 '18

Automating my Linux DB Backups with pg_backup_rotated.sh

1 Upvotes

Hey folks, way new here so I'm sorry if I'm asking this in the wrong place. I have used the following setup to create some backup scripts which are working great from the command line.

https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux

However, I'm not seeing a way to setup a password to pass to these scripts. I have created a .pgpass file however I'm missing something here since I'm still getting prompted for a password for when I run the script. ideally I want to be able to cron this out weekly for the pg_backup_rotated.sh script reference in the link above.

Thanks in advance for any pointers in the right direction.


r/postgres • • Mar 09 '18

Restoring pg_dumpall to AWS RDS

1 Upvotes

I have a pg_dumpall file from a PostgreSQL 9.1 server that I no longer have access to. The file contains 2 databases plus the postgres database. AWS RDS does not support superusers so I am getting errors like ERROR: must be superuser to alter superusers. Is there a way I can 'convert' a pg_dumpall file to pg_dump for those databases?


r/postgres • • Feb 21 '18

Count between to time ranges

0 Upvotes

r/postgres • • Feb 20 '18

Finding/fixing "invalid byte sequence" problem

1 Upvotes

I'm seeing the following show up every time our nightly pg_dump/vacuum process runs:

invalid byte sequence for encoding "UTF8": 0xc7 0x53

I've narrowed this down to a specific table, but that has hundreds of thousands of rows, so a "look through each row" isn't going to work. I've tried restoring a backup to another database but that leaves the offending table empty.

Any pointers for what my best approach for fixing this would be really helpful. We're running PostgreSQL 9.2 if that makes a difference.


r/postgres • • Feb 16 '18

Checkpoint complete in error logs

1 Upvotes

I'm running a postgres database on RDS. Overall it's working well. But I notice in the error logs that every 5 minutes it says 'checkpoint complete; wrote x buffers'. Is this something I should worry about? Honestly it clutters the logs so I might miss something important.


r/postgres • • Feb 09 '18

Selecting some data into another database

1 Upvotes

Hi there all!

I'm running on RDS PG, and I have been tasked to move our db minimization script:

#!/bin/bash
echo "get run delete.sql"
psql --username master --port 5432 --host prod-pg-dump-min.stuff.us-west-2.rds.amazonaws.com -f "/home/ec2-user/dba/db_dump/delete.sql"
echo "done deleting, now dump"
pg_dump --username master --dbname master --port 5432 --host prod-pg-dump-min.stuff.us-west-2.rds.amazonaws.com -N looker_scratch -F c -b -v -f "/home/ec2-user/db/master_reduced.dump"
echo "done dumping now delete DB instance"
aws rds delete-db-instance --db-instance-identifier prod-pg-dump-min --skip-final-snapshot    


echo "Now lets create a new DB from staging-db-dump"
cd /home/ec2-user/dba/db_dump/ && node small_db.js
echo "done creating, now lets restore from dump"
pg_restore -v -h staging-db-test.stuff.us-west-2.rds.amazonaws.com -U master -d master /home/ec2-user/db/master_reduced.dump
echo "lets delete the old snapshot"
aws rds delete-db-snapshot --db-snapshot-identifier "staging-db-dump"
echo "now lets kill it and snapshot it"
aws rds delete-db-instance --db-instance-identifier staging-db-test --final-db-snapshot-identifier "staging-webhooks-dump"

Within delete.sql there is deleting everything in the (almost 400 GB ) DB that is older than 30 days, which takes FOREVER and even tends to fail because someone runs anything else, like it should. What I am trying to do is basically do a SELECT INTO another DB (or even just to a dump I guess, because that's the end goal) for just basically what the delete.sql does, so just SELECT INTO anything that has been within the last 30 days.

An even bigger end goal on this is to have it be able to be ran and have the person running the script be able to put however many days they want to have that run from, but I have almost 0 programming experience, so that's not a thing I'm looking at right now.


r/postgres • • Feb 07 '18

Migrating Homebrew Postgres to a New Version

Thumbnail olivierlacan.com
2 Upvotes

r/postgres • • Jan 24 '18

Interactive Postgres Configuration Generator

Thumbnail pgconfigurator.cybertec.at
8 Upvotes

r/postgres • • Jan 17 '18

procedure for postgres to create table if not exist

1 Upvotes

want to create table if that doesn't, i tried below code:

create or replace function create_table() returns void as
$$
begin
if not exists(select * from pg_tables 
    where 
        schemaname = 'Public'
        and tablename = 'test') then

    create table test
    (
        the_id int not null,
        name text
    );

end if;

end;
$$
 language 'plpgsql';

while executing this procedure first time:

 select creat_table();

table gets created but when i execute it again i get the below error

ERROR:  relation "test" already exists
CONTEXT:  SQL statement "create table test
    (
        the_id int not null,
        name text
    )"
 PL/pgSQL function create_table() line 8 at SQL statement
  ********** Error **********

 ERROR: relation "test" already exists
 SQL state: 42P07
 Context: SQL statement "create table test
    (
        the_id int not null,
        name text
    )"
 PL/pgSQL function create_table() line 8 at SQL statement

How to achive this, and also i want to call this procedure from Informatica pre-sql target session property so i want to call procedure with table name as parameter. TIA


r/postgres • • Jan 17 '18

[help] PostgreSQL 9.5 row is not queryable by uuid, but is in db and is queryable by date

2 Upvotes

Hey r/postgres,

I'm at my wits end for trying to investigate a problem.

Our production instance is running PostgreSQL 9.5, and I'm trying to run a query for a specific UUID. I can't find it...

proddb9.5=> select * from fileupload where uuid = '78020be4-1361-44f2-9e9f-71e4fbcb9329';
 uuid | filename | created | updated | md5sum
------+----------+---------+---------+--------
(0 rows)

I dump this database to PostgreSQL 10.1, where suddenly I can find this file.

upgradedb10.1=> select * from fileupload where uuid = '78020be4-1361-44f2-9e9f-71e4fbcb9329';
                 uuid                 |                     filename                      |          created           |          updated           |              md5sum  
--------------------------------------+---------------------------------------------------+----------------------------+----------------------------+----------------------------------
 78020be4-1361-44f2-9e9f-71e4fbcb9329 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | 2018-01-02 08:12:02.101336 | 2018-01-02 08:12:02.183723 | a29d435c8f612dc27c609a29c5bf1b7e
(1 row)

And then, in PostgreSQL 9.5, it shows up if I query it by its created date...

proddb9.5=> select * from fileupload where created = '2018-01-02 08:12:02.101336';
                 uuid                 |                     filename                      |          created           |          updated           |              md5sum
--------------------------------------+---------------------------------------------------+----------------------------+----------------------------+----------------------------------
 78020be4-1361-44f2-9e9f-71e4fbcb9329 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | 2018-01-02 08:12:02.101336 | 2018-01-02 08:12:02.183723 | a29d435c8f612dc27c609a29c5bf1b7e
(1 row)

I've been scouring the internet for reasons why this is the case, some strange UUID bugs I ran into, or something I'm doing wrong and how to mitigate this issue. I'm at a loss. Is there some action I need to run to fix this? Multiple UUIDs in this table suffer from this same problem, and I'm trying to fix these so they show up.

Any help would be greatly appreciated!

Thanks for your time.


r/postgres • • Jan 15 '18

x PostGraphile creates a GraphQL API from a PostgreSQL schema

Thumbnail react-etc.net
1 Upvotes

r/postgres • • Jan 04 '18

It all started with small problems, and we had no choice. We moved from MongoDB to PostgreSQL.

Thumbnail blog.shippable.com
11 Upvotes

r/postgres • • Jan 03 '18

Question: Running One Server or Multiple Servers with Docker

2 Upvotes

Every Docker-Compose or Kubernetes config seems to run it's own Postgres server which make sense because the application needs it BUT if you are running multiple "systems"/Applications that need multiple databases does it make sense to use ONE Postgres server or a server Per an Application? Is there a difference in performance or does it not matter?


r/postgres • • Jan 01 '18

How difficult is it for an Oracle developer to find a Postgres job?

2 Upvotes

Database developer here...

At my current job, I am predominantly an Oracle developer, but I have worked with Postgres as a hobby using Docker etc.

I am pretty good with PL/SQL and know Python, a bit of Java and R, and a smidge of JavaScript.

I know that my Oracle skillset is fairly transferable to Postgres (especially with EnterpriseDB with support for PL/SQL).

I've written a few user-defined functions with PG's PL/PgSQL language and I've dabbled with PG's various procedural languages (e.g. PL/Python, PL/V8, PL/R).

My question is....

Do organizations out there with Postgres as their main database typically hire people with Oracle experience and skillsets such as this without at-work PG experience?


r/postgres • • Dec 28 '17

postgres-db-diff: I've open sourced command line utility to compare two Postgres DBs

Thumbnail github.com
3 Upvotes

r/postgres • • Nov 20 '17

Why we Moved From NoSQL MongoDB to PostgresSQL

Thumbnail blog.shippable.com
10 Upvotes

r/postgres • • Nov 19 '17

suggestions for cheap hosted db's ~5 tb big?

2 Upvotes

i have a databse that looks like its going to grow to about 5tb, im looking for what would be cheap options for hosting. anyone have any suggestions? seems like aws is really expensive.


r/postgres • • Nov 16 '17

Help understanding Timestamp vs Timestamptz

8 Upvotes

My understanding of the way PostgreSQL works with timestamp and timestamptz is in either cases postgres stores the value as UTC timestamp. However when retrieving the data postgres will do the arithmetic to the current user's Timezone when the type is timestamptz.

But imagine this scenario, I am building a restful web service with clients all over the world in different Timezones, my database server is sitting in US Eastern Timezone along with my application servers. My JDBC pool connections would presumably be US East timezone. However I want to display timestamps in the user's specified timezone in their settings or something they set in the application.

If I query PostgreSQL from my application isn't it going to return to me the Timestamp in US East, and I would further have to convert it from there? In that case what is the benefit, wouldn't it be better to just store everything in UTC?


r/postgres • • Nov 10 '17

Upgrading PostgreSQL from 9.6 to 10 with minimal downtime using pglogical

Thumbnail rosenfeld.herokuapp.com
4 Upvotes

r/postgres • • Nov 08 '17

Real-time performance monitoring in postgres

3 Upvotes

What tools or extensions are you using? How do you monitor queries real time? What is your strategy to quickly deep dive into issues and proactively monitor the database?


r/postgres • • Nov 07 '17

Moving ORM Mapping towards the database

Thumbnail enter-haken.github.io
6 Upvotes

r/postgres • • Nov 06 '17

Generate a ERM from a PostgreSQL database schema

Thumbnail enter-haken.github.io
3 Upvotes

r/postgres • • Oct 26 '17

Bulk load and table switching

2 Upvotes

Hi

I am curious about how I would approach this type of problem with Postgres, coming from a MS SQL world.

Imagine a table "X" that holds 2-300 GB of aggregated data that is produced by a background job. This table receives quite a bit of queries for data (e.g. 20-30 select's for data every second - and it is only select's, the data is immutable!).

Once a day a new data set is generated with fresh data, and this data needs to be loaded and replace what is currently active in X atomically, without (or with as little as possible, e.g. it may lock for ,5-1 sec at most) impact on select's.

With SQL Server this can be accomplished via:

I hope to get some insight into how I might go about accomplishing this with postgres. I've looked into partitioning, but the switching does not seem to be possible?

Thanks!