# Postgres

Download Postgres: [PostgreSQL: Downloads](https://www.postgresql.org/download/)

### Set up database credentials

```bash
postgres=# \password new_name
postgres=# CREATE DATABASE db_name;
```

### Connection

```python
from psycopg2 import sql

conn = psycopg2.connect(
    user = "new_name",
    password="password",
    host="127.0.0.1",
    port="port_number",
    database="db_name"
)
cursor = conn.cursor()
```

### Commit Changes

```python
conn.commit()
```

### Create Table

```python
table = (
        """
        CREATE TABLE IF NOT EXISTS
        table (
            id SERIAL PRIMARY KEY,
            name TEXT,
            date DATE,
            status BOOLEAN,
            num INTEGER,
            UNIQUE (num)
        );
        """
)
cursor.execute(table)
```

### Insert

```python
insert = """
    INSERT INTO 
    table (name, date)
    VALUES (%s, %s)
"""
cursor.execute(insert, ("Hello", "2014-09-03"))
```

### Update

```python
update = """
    UPDATE table 
    SET name = %s,
        date = %s
    WHERE num = %s
"""
cursor.execute(update, ("Hi", "2019-03-20"))
```

### Select

```python
cursor.execute("""
    SELECT num FROM table 
    WHERE num = %s;
""", (2, ))
```

### Get value in row

```python
cursor.fetchone()[column_num]
```

### Rollbacks

```python
cursor.execute('SAVEPOINT sp2')

try:
  cursor.execute("""
    SELECT num FROM table 
    WHERE num = %s;
  """, (2, ))
except psycopg2.IntegrityError:
  cursor.execute('ROLLBACK TO SAVEPOINT sp2')
```

## Convert Postgres Database to CSV

```sql
COPY table_name/query TO '/Users/laura/...path_to/file.csv' DELIMITER ',' CSV HEADER;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lauradang.gitbook.io/notes/databases/postgres.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
