Adopting PostgreSQL (DRF)

JunePyo Suh·2020년 9월 9일
0

PostgreSQL installation to Mac OS

1. Download PostgreSQL installation package from Postgres.app or with HomeBrew.

2. In case of using Postgres.app, move the downloaded package to Applications folder and click "initialize" to create a new server

3. Configure $PATH to use the included command line tools:

sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

4. The default settings for the PostgreSQL server running on your Mac will be:

5. In your settings.py (Django):

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "[YOUR_DATABASE_NAME]",
        "USER": "[YOUR_USER_NAME]",
        "PASSWORD": "",
        "HOST": "localhost",
        "PORT": "",
    }
}

6. Install PostgreSQL package for python

pip install psycopg2

Some Usage Tips

Using ArrayField

order = ArrayField(models.IntegerField(), default=list)
 order = serializers.ListField(
        child=serializers.IntegerField(), required=False)

If you would like to connect to the database and make changes:

PostgreSQL arrays are enclosed by {}, not []

conn = psycopg2.connect("dbname=name user=username")
cur = conn.cursor()
self.order = cur.mogrify("SELECT %s;", (order, ))

More about psycopg2 usage can be found here

0개의 댓글