0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3509128
  • 142
  • 1
  • 10

2 Answers2

1

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.

Community
  • 1
  • 1
Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
0

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.

Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69