๐Ÿ SQL ์‹œ์ž‘ํ•˜๊ธฐ

8069 ๋‹จ์–ด

โœ”๏ธ PostgreSQL ์„ค์น˜



๋จผ์ € ๋‹ค์Œ ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์Šคํ…œ ์†Œํ”„ํŠธ์›จ์–ด ํŒจํ‚ค์ง€๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.


sudo apt update



๊ธฐ๋ณธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ์ตœ์‹  ๋ฒ„์ „์˜ PostgreSQL์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.


sudo apt install postgresql



์„ค์น˜ ํ›„ PostgreSQL ์„œ๋น„์Šค๊ฐ€ ํ™œ์„ฑ ์ƒํƒœ์ธ์ง€, ์‹คํ–‰ ์ค‘์ธ์ง€, ํ™œ์„ฑํ™”๋˜์–ด ์žˆ๋Š”์ง€ ๋˜๋Š” ๋‹ค์Œ ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์—ฐ๊ฒฐ์„ ์ˆ˜๋ฝํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


sudo systemctl is-active postgresql  

sudo systemctl is-enabled postgresql 

sudo systemctl status postgresql 

sudo pg_isready



โœ”๏ธ pgAdmin ์„ค์น˜



pgAdmin์€ PostgreSQL์šฉ ๊ด€๋ฆฌ ๋„๊ตฌ์ด์ง€๋งŒ Ubuntu ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ pgAdmin4 APT ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ์„ค์น˜ํ•˜๊ณ  ๊ณต๊ฐœ ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


curl https://www.pgadmin.org/static/packages_pgadmin_org.pub

sudo apt-key add

sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'



๊ทธ๋Ÿฐ ๋‹ค์Œ ์„ค์น˜


sudo apt install pgadmin4



โœ”๏ธ NodeJ ๋ฐ node-postgres ์„ค์น˜


node-postgres๋Š” PostgreSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šคํ•˜๊ธฐ ์œ„ํ•œ node.js ๋ชจ๋“ˆ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค.


sudo apt install nodejs

sudo apt install npm  

npm install pg



๐Ÿ”Œ PostgreSQL๊ณผ NodeJ ์—ฐ๊ฒฐํ•˜๊ธฐ




const { Pool } = require('pg');

const pool = new Pool({
    user: 'lessa',
    password: '88eb9394',
    host: '/var/run/postgresql',
    port: 5432,
    database: 'service_management',
});

pool.on('error', (err, client) => {
    console.error('Unexpected error on idle client', err)
    process.exit(-1)
})

const startDatabase = (req, res, query, values) => {
    pool.connect()
        .then(client => {
            return client.query(query, values)
                .then(result => {
                    client.release();
                    res.status(200).send(result.rows)
                })
                .catch(err => {
                    client.release();
                    res.sendStatus(500);

                })
        })
}




๐Ÿ”ป pg๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์˜ˆ์‹œ

const getCustomer = (req, res) => {
    const customer = [req.params.name];
    let query = 'SELECT * FROM customer WHERE deleted_date IS NULL'

    if (customer) {
        query += " AND name LIKE '%'||$1||'%'"
    }

    startDatabase(req, res, query, customer)
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ