"Show tables like" in postgresql - psql

Is there any way to list all tables that contain certain keywords in psql?
In other words, is there a similar command as in mysql:
show tables like "%test%" ?

Just found the answer..
\dt *test* will pull tables: 123_test_234, test_234, 123_test etc.
\dt *test will pull tables: 123_test etc.
\dt test* will pull tables: test_234 etc.
Not sure if there is a similar question. If there is, maybe I should delete this.

Related

Strange Assignee names coming from Assignee column in jiraissue table

I am using Jira backend database to pull some columns for reporting. Assignee column in jiraissue table stores the Webkey ID/usernames of all the employees. I join this with cwd_user to get the full name of the assignee. But I also see some weird values like the ones below. I don't have a clue about how to get the display names of these users. They are not Webkey ID/usernames.
Any thoughts on what I might be missing?
The app_user table has the current userid in the lower_user_name column
If you're running a recent version of Jira, there was a GDPR-related change that causes all newly-created users to have a key starting with JIRAUSER instead of their username. Users can also be anonymized through this feature.
You can get a mapping of JIRAUSER keys to usernames through the JIRA API's Get User endpoint -- not sure where to look in the database for this mapping yet.

Get DDL of all tables in a postgresql schema

I need to get the "CREATE TABLE" statements for all the tables in a particular schema of postgreSQL. How can I do so? Thanks in advance.

Join Case and User in Salesforce (SOQL)

I'm used to PostgreSQL but very new to SOQL and a bit confused about it. I would like to join rows from Case with rows from User based on User.Id.
I've looked online and tried various solutions but nothing worked out.
Does anyone know I could do that? What type of relationship binds Case and User? Do I have to build a custom relationship between them?
Thanks
SOQL supports joins by following the relationship from the FK (using the relationship name), many of the schema explorer tools (SoqlX, Workbench, etc) will help you discover these and build/test SOQL queries.
In this case you want something like
select CaseNumber, LastModifiedBy.Name from Case Where ....
Try this, https://salesforce.stackexchange.com/questions/20572/soql-join-between-two-standard-objects
Select CaseNumber, LastModifiedById, (Select name From User)
From Case WHERE day_only(convertTimezone(ClosedDate))=TODAY

Export postgres table based on rails relation

I have a user table that I can easily export with postgres by doing
\copy users to 'tmp/users2015.csv' csv header
My users has_many questions, and I'm wondering if there's a way I can export just the users who have asked a question. The problem is here's no column in my users table that represents the number of questions a user has asked, so I don't think I can do this. Is there any possible way or a different approach? I really care about just knowing how many users have asked questions, so I could also export my questions table, and possibly filter out questions that have been asked by the same user?
You can execute the same select query that the has_many relationship uses and use that to write to a file.
Copy (Select * From users where <some condition>) To '/tmp/test.csv' With CSV
See Save PL/pgSQL output from PostgreSQL to a CSV file for more info.
Your should export in additional to the user table a table which has a belongs_to field, for example of question table, to 'tmp/question2015.csv' CSV.

Rails, Active Record - number of rows in tables

I got an email from Heroku saying I have too many rows in my Postgres DB.
How can I see how many rows I have in each table (so I can prioritize deletion)?
heroku pg:psql (specify database name here if you have more than one)
Then check out this post to get a row count in postgres
How do you find the row count for all your tables in Postgres
A better way to look at this might be:
How many records do I have for each of my AR models?
While it may not exactly match the numbers Heroku gives (may be a bit lower), and may have some extra stuff (that are not models), but for practical purposes, this is best option and it avoids the need to use SQL.
ActiveRecord::Base.descendants.map { |d| [d.all.size, d.name] }.sort
Explanation:
ActiveRecord::Base.descendants is a quick way to get all of the AR models. This is used to create an array of arrays that contains the [number of records, model name] to allow for simple lexicographic sorting. This should be enough to quickly determine where all your rows are going.

Resources