[{"data":1,"prerenderedAt":372},["ShallowReactive",2],{"content-query-HJQkgdp364":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"tags":10,"body":11,"_type":366,"_id":367,"_source":368,"_file":369,"_stem":370,"_extension":371},"/tutorials/psql","tutorials",false,"","PostgreSQL database access with psql","Assuming your user has postgres privileges, access database \u003Cdb_name> with:","postgres",{"type":12,"children":13,"toc":363},"root",[14,23,38,89,94,117,122,136,140,146,166,245,251,256,357],{"type":15,"tag":16,"props":17,"children":19},"element","h1",{"id":18},"connecting-to-a-postgres-database",[20],{"type":21,"value":22},"text","Connecting to a Postgres database",{"type":15,"tag":24,"props":25,"children":26},"p",{},[27,29,36],{"type":21,"value":28},"Assuming your user has postgres privileges, access database ",{"type":15,"tag":30,"props":31,"children":33},"code",{"className":32},[],[34],{"type":21,"value":35},"\u003Cdb_name>",{"type":21,"value":37}," with:",{"type":15,"tag":39,"props":40,"children":44},"pre",{"className":41,"code":42,"language":43,"meta":7,"style":7},"language-bash shiki shiki-themes github-light monokai monokai github-light monokai","psql -d \u003Cdb_name>\n","bash",[45],{"type":15,"tag":30,"props":46,"children":47},{"__ignoreMap":7},[48],{"type":15,"tag":49,"props":50,"children":53},"span",{"class":51,"line":52},"line",1,[54,60,66,72,78,84],{"type":15,"tag":49,"props":55,"children":57},{"style":56},"--shiki-default:#6F42C1;--shiki-dark-mode:#A6E22E;--shiki-sky-dark-mode:#A6E22E;--shiki-light-mode:#6F42C1;--shiki-blue-mode:#A6E22E",[58],{"type":21,"value":59},"psql",{"type":15,"tag":49,"props":61,"children":63},{"style":62},"--shiki-default:#005CC5;--shiki-dark-mode:#AE81FF;--shiki-sky-dark-mode:#AE81FF;--shiki-light-mode:#005CC5;--shiki-blue-mode:#AE81FF",[64],{"type":21,"value":65}," -d",{"type":15,"tag":49,"props":67,"children":69},{"style":68},"--shiki-default:#D73A49;--shiki-dark-mode:#F92672;--shiki-sky-dark-mode:#F92672;--shiki-light-mode:#D73A49;--shiki-blue-mode:#F92672",[70],{"type":21,"value":71}," \u003C",{"type":15,"tag":49,"props":73,"children":75},{"style":74},"--shiki-default:#032F62;--shiki-dark-mode:#E6DB74;--shiki-sky-dark-mode:#E6DB74;--shiki-light-mode:#032F62;--shiki-blue-mode:#E6DB74",[76],{"type":21,"value":77},"db_nam",{"type":15,"tag":49,"props":79,"children":81},{"style":80},"--shiki-default:#24292E;--shiki-dark-mode:#F8F8F2;--shiki-sky-dark-mode:#F8F8F2;--shiki-light-mode:#24292E;--shiki-blue-mode:#F8F8F2",[82],{"type":21,"value":83},"e",{"type":15,"tag":49,"props":85,"children":86},{"style":68},[87],{"type":21,"value":88},">\n",{"type":15,"tag":24,"props":90,"children":91},{},[92],{"type":21,"value":93},"For instance, to access a NELA database:",{"type":15,"tag":39,"props":95,"children":97},{"className":41,"code":96,"language":43,"meta":7,"style":7},"psql -d nela-eng\n",[98],{"type":15,"tag":30,"props":99,"children":100},{"__ignoreMap":7},[101],{"type":15,"tag":49,"props":102,"children":103},{"class":51,"line":52},[104,108,112],{"type":15,"tag":49,"props":105,"children":106},{"style":56},[107],{"type":21,"value":59},{"type":15,"tag":49,"props":109,"children":110},{"style":62},[111],{"type":21,"value":65},{"type":15,"tag":49,"props":113,"children":114},{"style":74},[115],{"type":21,"value":116}," nela-eng\n",{"type":15,"tag":24,"props":118,"children":119},{},[120],{"type":21,"value":121},"To list the existing databases in a Postgres server use:",{"type":15,"tag":39,"props":123,"children":125},{"className":41,"code":124,"language":43,"meta":7,"style":7},"\\l\n",[126],{"type":15,"tag":30,"props":127,"children":128},{"__ignoreMap":7},[129],{"type":15,"tag":49,"props":130,"children":131},{"class":51,"line":52},[132],{"type":15,"tag":49,"props":133,"children":134},{"style":56},[135],{"type":21,"value":124},{"type":15,"tag":137,"props":138,"children":139},"hr",{},[],{"type":15,"tag":16,"props":141,"children":143},{"id":142},"saving-queries-to-a-csv-file",[144],{"type":21,"value":145},"Saving queries to a CSV file",{"type":15,"tag":24,"props":147,"children":148},{},[149,151,156,158,164],{"type":21,"value":150},"Once connected to a database in ",{"type":15,"tag":30,"props":152,"children":154},{"className":153},[],[155],{"type":21,"value":59},{"type":21,"value":157},", you can use ",{"type":15,"tag":30,"props":159,"children":161},{"className":160},[],[162],{"type":21,"value":163},"\\copy",{"type":21,"value":165}," to save query results to a file:",{"type":15,"tag":39,"props":167,"children":169},{"className":41,"code":168,"language":43,"meta":7,"style":7},"\\copy (SELECT title, content FROM articles WHERE date_published >= '2025-01-01') to '/path/to/file.csv' with csv header;\n",[170],{"type":15,"tag":30,"props":171,"children":172},{"__ignoreMap":7},[173],{"type":15,"tag":49,"props":174,"children":175},{"class":51,"line":52},[176,180,185,190,195,200,205,210,215,220,225,230,235,240],{"type":15,"tag":49,"props":177,"children":178},{"style":56},[179],{"type":21,"value":163},{"type":15,"tag":49,"props":181,"children":182},{"style":80},[183],{"type":21,"value":184}," (SELECT ",{"type":15,"tag":49,"props":186,"children":187},{"style":74},[188],{"type":21,"value":189},"title,",{"type":15,"tag":49,"props":191,"children":192},{"style":74},[193],{"type":21,"value":194}," content",{"type":15,"tag":49,"props":196,"children":197},{"style":74},[198],{"type":21,"value":199}," FROM",{"type":15,"tag":49,"props":201,"children":202},{"style":74},[203],{"type":21,"value":204}," articles",{"type":15,"tag":49,"props":206,"children":207},{"style":74},[208],{"type":21,"value":209}," WHERE",{"type":15,"tag":49,"props":211,"children":212},{"style":74},[213],{"type":21,"value":214}," date_published",{"type":15,"tag":49,"props":216,"children":217},{"style":68},[218],{"type":21,"value":219}," >",{"type":15,"tag":49,"props":221,"children":222},{"style":74},[223],{"type":21,"value":224},"=",{"type":15,"tag":49,"props":226,"children":227},{"style":74},[228],{"type":21,"value":229}," '2025-01-01'",{"type":15,"tag":49,"props":231,"children":232},{"style":80},[233],{"type":21,"value":234},") to ",{"type":15,"tag":49,"props":236,"children":237},{"style":74},[238],{"type":21,"value":239},"'/path/to/file.csv'",{"type":15,"tag":49,"props":241,"children":242},{"style":80},[243],{"type":21,"value":244}," with csv header;\n",{"type":15,"tag":16,"props":246,"children":248},{"id":247},"inner-join-example-creating-a-random-sample-of-a-nela-database",[249],{"type":21,"value":250},"INNER JOIN example creating a random sample of a NELA database",{"type":15,"tag":24,"props":252,"children":253},{},[254],{"type":21,"value":255},"This example will save 1000 random entries from a NELA database and save it to a CSV file.",{"type":15,"tag":39,"props":257,"children":259},{"className":41,"code":258,"language":43,"meta":7,"style":7},"\\copy (SELECT * FROM (articles INNER JOIN sources on articles.source_id=sources.id) WHERE date_published >= '2024-01-01' ORDER BY RANDOM() LIMIT 1000) to '/path/to/file.csv' with csv header;\n",[260],{"type":15,"tag":30,"props":261,"children":262},{"__ignoreMap":7},[263],{"type":15,"tag":49,"props":264,"children":265},{"class":51,"line":52},[266,270,274,280,284,289,294,299,304,309,314,319,324,329,334,339,344,349,353],{"type":15,"tag":49,"props":267,"children":268},{"style":56},[269],{"type":21,"value":163},{"type":15,"tag":49,"props":271,"children":272},{"style":80},[273],{"type":21,"value":184},{"type":15,"tag":49,"props":275,"children":277},{"style":276},"--shiki-default:#005CC5;--shiki-dark-mode:#FD971F;--shiki-sky-dark-mode:#FD971F;--shiki-light-mode:#005CC5;--shiki-blue-mode:#FD971F",[278],{"type":21,"value":279},"*",{"type":15,"tag":49,"props":281,"children":282},{"style":74},[283],{"type":21,"value":199},{"type":15,"tag":49,"props":285,"children":286},{"style":80},[287],{"type":21,"value":288}," (articles ",{"type":15,"tag":49,"props":290,"children":291},{"style":74},[292],{"type":21,"value":293},"INNER",{"type":15,"tag":49,"props":295,"children":296},{"style":74},[297],{"type":21,"value":298}," JOIN",{"type":15,"tag":49,"props":300,"children":301},{"style":74},[302],{"type":21,"value":303}," sources",{"type":15,"tag":49,"props":305,"children":306},{"style":74},[307],{"type":21,"value":308}," on",{"type":15,"tag":49,"props":310,"children":311},{"style":74},[312],{"type":21,"value":313}," articles.source_id=sources.id",{"type":15,"tag":49,"props":315,"children":316},{"style":80},[317],{"type":21,"value":318},") WHERE date_published ",{"type":15,"tag":49,"props":320,"children":321},{"style":68},[322],{"type":21,"value":323},">",{"type":15,"tag":49,"props":325,"children":326},{"style":80},[327],{"type":21,"value":328},"= ",{"type":15,"tag":49,"props":330,"children":331},{"style":74},[332],{"type":21,"value":333},"'2024-01-01'",{"type":15,"tag":49,"props":335,"children":336},{"style":80},[337],{"type":21,"value":338}," ORDER BY ",{"type":15,"tag":49,"props":340,"children":341},{"style":56},[342],{"type":21,"value":343},"RANDOM",{"type":15,"tag":49,"props":345,"children":346},{"style":80},[347],{"type":21,"value":348},"() LIMIT 1000) to ",{"type":15,"tag":49,"props":350,"children":351},{"style":74},[352],{"type":21,"value":239},{"type":15,"tag":49,"props":354,"children":355},{"style":80},[356],{"type":21,"value":244},{"type":15,"tag":358,"props":359,"children":360},"style",{},[361],{"type":21,"value":362},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark-mode .shiki span {color: var(--shiki-dark-mode);background: var(--shiki-dark-mode-bg);font-style: var(--shiki-dark-mode-font-style);font-weight: var(--shiki-dark-mode-font-weight);text-decoration: var(--shiki-dark-mode-text-decoration);}html.dark-mode .shiki span {color: var(--shiki-dark-mode);background: var(--shiki-dark-mode-bg);font-style: var(--shiki-dark-mode-font-style);font-weight: var(--shiki-dark-mode-font-weight);text-decoration: var(--shiki-dark-mode-text-decoration);}html .sky-dark-mode .shiki span {color: var(--shiki-sky-dark-mode);background: var(--shiki-sky-dark-mode-bg);font-style: var(--shiki-sky-dark-mode-font-style);font-weight: var(--shiki-sky-dark-mode-font-weight);text-decoration: var(--shiki-sky-dark-mode-text-decoration);}html.sky-dark-mode .shiki span {color: var(--shiki-sky-dark-mode);background: var(--shiki-sky-dark-mode-bg);font-style: var(--shiki-sky-dark-mode-font-style);font-weight: var(--shiki-sky-dark-mode-font-weight);text-decoration: var(--shiki-sky-dark-mode-text-decoration);}html .light-mode .shiki span {color: var(--shiki-light-mode);background: var(--shiki-light-mode-bg);font-style: var(--shiki-light-mode-font-style);font-weight: var(--shiki-light-mode-font-weight);text-decoration: var(--shiki-light-mode-text-decoration);}html.light-mode .shiki span {color: var(--shiki-light-mode);background: var(--shiki-light-mode-bg);font-style: var(--shiki-light-mode-font-style);font-weight: var(--shiki-light-mode-font-weight);text-decoration: var(--shiki-light-mode-text-decoration);}html .blue-mode .shiki span {color: var(--shiki-blue-mode);background: var(--shiki-blue-mode-bg);font-style: var(--shiki-blue-mode-font-style);font-weight: var(--shiki-blue-mode-font-weight);text-decoration: var(--shiki-blue-mode-text-decoration);}html.blue-mode .shiki span {color: var(--shiki-blue-mode);background: var(--shiki-blue-mode-bg);font-style: var(--shiki-blue-mode-font-style);font-weight: var(--shiki-blue-mode-font-weight);text-decoration: var(--shiki-blue-mode-text-decoration);}",{"title":7,"searchDepth":364,"depth":364,"links":365},2,[],"markdown","content:tutorials:psql.md","content","tutorials/psql.md","tutorials/psql","md",1793934184831]